【发布时间】:2018-11-13 13:29:47
【问题描述】:
我有这个代码,但我不能让它工作。输入行根本不接受任何东西。我试着到处搜索都无济于事,所以我决定最后问这个问题。 附言我是新来的反应
class App extends React.Component {
state = { inputValue: [{item:'', name:''}] }
handleChange = e => {
const newValue = [...this.state.inputValue];
newValue[0][e.target.name] = e.target.value;
this.setState({inputValue: newValue});
}
render(){
return(
<div className='container jumbotron'>
<div className="row">
<div className="col">
<FirstInput handleChange={this.handleChange} inputValue={this.state.inputValue[0].name}/>
</div>
<div className="col">
<SecondInput handleChange={this.handleChange} inputValue={this.state.inputValue[0].name}/>
</div>
</div>
</div>
);
}
}
const FirstInput = (props) => (
<div>
<label>First Input</label>
<input className="form-control" onChange={props.handleChange} value={props.inputValue}/>
</div>
)
const SecondInput = ({inputValue, handleChange}) => (
<div>
<label>Second Input</label>
<input className="form-control" onChange={handleChange} value={inputValue}/>
</div>
)
ReactDOM.render(<App />, document.getElementById('root'));
对不起,我忘了提到我想将数组维护为对象数组。目标是使第一个输入和第二个输入具有相同的值。意思是,更改一个输入将使另一个输入相同。
【问题讨论】:
-
问题在于
newValue[0][e.target.name]您的输入没有名称。我想你的意思是newValue[0][name]
标签: javascript reactjs input data-binding