简体   繁体   中英

Coordinated input in all text boxes

I have an exercise and I do not understand how to do it.

Build a control with 5 text boxes that the input in all text boxes is correlated. Changing input in one box changes the input in the rest of the box.

class App extends Component {
 render() {
    return (
      <div>
        <MultiInput />
      </div>
    );
  }
}

function SameInput(props) {
  return <input type="text" name="sameInput"/>
}

class MultiInput extends React.Component {
  constructor(props) {
    super(props);
    this.state = { value: "ho" };
  }

  onChange = (e) => {
    this.setState({ value: e.target.value });
  }

  render() {
    return <div>
      <SameInput value={this.state.value} onChange={this.onChange}></SameInput><br />
      <SameInput value={this.state.value} onChange={this.onChange}></SameInput><br />
      <SameInput value={this.state.value} onChange={this.onChange}></SameInput><br />
    </div>
  }
}

export default App;

You are passing onChange event handler function and value to SameInput functional component but you are not actually handling them in input text field in SameInput function of input element.

Try with below change it would work

 function SameInput(props) {
     return <input type="text" name="sameInput" onChange={props.onChange} value={props.value}/>
  }

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