如果你使用@Tholle的建议,那么你应该使用
updateInput
功能如下:
updateInput(title, value) {
console.log( title, value );
}
title
子组件的父组件中的状态。下面是一个示例:
class App extends React.Component {
state = {
title: "",
}
updateInput = title => {
this.setState( { title });
}
render() {
return (
<div>
<Input title={this.state.title} onChange={this.updateInput} />
<br />
Title is: {this.state.title}
</div>
);
}
}
const Input = (props) => {
const handleInput = e =>
props.onChange(e.target.value)
return (
<input
className="text"
required
onChange={handleInput}
value={props.title}
/>
);
}
ReactDOM.render(
<App />,
document.getElementById("root")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>