简体   繁体   中英

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>
    )
}

you need to pass the index as well

In the parent

onChange={this.handleChange}

In the child

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM