我正在学习在 React 中思考,但不明白为什么示例中的 SearchBar 需要具有value={this.props.filterText}
and checked={this.props.inStockOnly}
,jsFiddle 在没有它们的情况下仍然可以工作,并且将道具传递给 SearchBar 没有意义,因为 Search 是处理用户输入并更改状态的一种。用户输入将反映在输入的值中,而没有设置为this.props.filterText
那为什么会存在呢?
var SearchBar = React.createClass({
handleChange: function() {
this.props.onUserInput(
this.refs.filterTextInput.value,
this.refs.inStockOnlyInput.checked
);
},
render: function() {
return (
<form>
<input
type="text"
placeholder="Search..."
value={this.props.filterText}
ref="filterTextInput"
onChange={this.handleChange}
/>
<p>
<input
type="checkbox"
checked={this.props.inStockOnly}
ref="inStockOnlyInput"
onChange={this.handleChange}
/>
{' '}
Only show products in stock
</p>
</form>
);
}
});