【发布时间】:2017-09-07 15:26:05
【问题描述】:
我使用复选框创建了一个基本界面,该界面使用了一种反应设计模式,这种模式在我之前很受用,并且我认为效果很好 - 即提升状态并将道具传递给 UI 组件。我的复选框组件传递了一个值(一个指标)、一个状态更改方法和一个用于检查的布尔值。问题是复选框不会立即更新,即使您可以在 React 开发工具中看到它们正在更新。它们仅在下次单击时更新,例如选中另一个复选框时。这是代码:
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
metricsSelected: []
}
this.selectMetric = this.selectMetric.bind(this)
}
selectMetric(metric) {
const metricsSelected = this.state.metricsSelected
const index = metricsSelected.indexOf(metric)
if (index !== -1){
metricsSelected.splice(index, 1)
}
else {
metricsSelected.push(metric)
}
this.setState({
metricsSelected,
});
}
render() {
return (
<div>
<Sidebar
metricsSelected={this.state.metricsSelected}
selectMetric={this.selectMetric}/>
<SomethingElse/>
</div>
)
}
}
const SomethingElse = () => (<div><h2>Something Else </h2></div>)
const Sidebar = ({ metricsSelected, selectMetric }) => {
const metrics = ['first thing', 'second thing', 'third thing']
return (
<div>
<h3>Select Metrics</h3>
{ metrics.map( (metric, i) =>
<Checkbox
key={i}
metric={metric}
selectMetric={selectMetric}
checked={metricsSelected.includes(metric)}/>
)}
</div>
)
}
const Checkbox = ({ metric, selectMetric, checked }) => {
const onChange = e => {
e.preventDefault()
selectMetric(e.target.value)
}
return (
<ul>
<li>{metric}</li>
<li><input
type='checkbox'
value={metric}
checked={checked}
onChange={onChange} /></li>
</ul>
)
}
我已经阅读了几乎所有关于反应复选框的信息,并且复选框的大多数应用程序都在做与我想做的不同的事情。我尝试向 Checkbox 组件添加状态,但这似乎没有帮助,因为选中的值仍然需要从其他地方进入。我认为当道具改变时反应组件会重新渲染。是什么赋予了?
【问题讨论】:
-
做了一些更改,检查一下它会非常快:codepen.io/anon/pen/xdKegq?editors=0010。主要问题是
e.preventDefault()删除它。