tryingCode
tryingCode

Reputation: 21

Passing a variable from Child component to Parent component

I am not getting the index from the event => { this.handleChange(event, index) } from the Child component, and I suspect the event isn't arriving either. I don't know how to transfer it up to the parent. I have tried adding it into the Child's props.onChange(event, index) but that didn't work. And i've fiddled as much as I can but am stuck. Any help would be greatly appreciated!

Parent:

 <Inputs hasInputs={hasInputs} onSubmit={this.handleSubmit} thoughtProp={this.state.thought} onChange={event => { this.handleChange(event, index) }} />

Child:


export const Inputs = (props) => {
    return (
        <form className="flex-item-main form" onSubmit={props.onSubmit}>
        <div>
                    <h4>
                      <p className='inputsHeader'>Thoughts:</p>  
                    </h4>
                </div>
            <ol>
              {props.thoughtProp.map((input, index) => (
                <Input type='text' key={index} value={input} onChange={props.onChange} className='textInputs' />
              ))}
              { props.hasInputs ? (
                <input className='submitThoughts' type='submit' value='Submit Thought!' />
              ) : (
                null
              )}
            </ol>
          </form>
    )
}

Upvotes: 0

Views: 67

Answers (1)

ludwiguer
ludwiguer

Reputation: 2245

you need to pass the index as well

In the parent

onChange={this.handleChange}

In the child

onChange={(e) => props.onChange(e, index)}

Upvotes: 1

Related Questions