[英]React - Pass an array into setState?
How would I pass an array into a setState
? 我如何将数组传递给
setState
? As you can see there is a checkbox that renders and once onchange
is called the id is put into the state but if there are multiple checkboxes it overwrites each time onchange
is called. 如您所见,有一个渲染的复选框,一旦调用
onchange
,ID就会进入状态,但是如果有多个复选框,则每次调用onchange
它都会被覆盖。
getInitialState() {
return {
listid: []
}
},
handleChange() {
this.setState({
listid: e.target.name,
});
},
renderCheckbox() {
const data = this.state.data || [];
return data.map((list, i) => {
return (
<Checkbox
key={i}
onChange={this.handleChange}
name={list.id}
value="Yes"
label={description}
/>
);
});
},
You could check in your handleChange
function if the checkbox id is present in the listid
array. 如果
listid
数组中存在复选框ID,则可以签入handleChange
函数。 If it is, you remove it. 如果是,则将其删除。 If it isn't, you add it.
如果不是,则添加它。
You can then check if the checkbox id is present in the listid
array in the render method to decide if it should be checked or not. 然后,您可以检查render方法中
listid
数组中是否存在复选框ID,以决定是否应对其进行检查。
Example 例
class App extends React.Component { state = { checkboxes: [{ id: "1" }, { id: "2" }, { id: "3" }, { id: "4" }], listIds: [] }; handleChange = event => { const { name } = event.target; this.setState(prevState => { const { listIds } = prevState; if (listIds.includes(name)) { return { listIds: listIds.filter(id => id !== name) }; } else { return { listIds: [...listIds, name] }; } }); }; render() { const { checkboxes, listIds } = this.state; return ( <div> {checkboxes.map(checkbox => ( <input key={checkbox.id} type="checkbox" checked={listIds.includes(checkbox.id)} name={checkbox.id} onChange={this.handleChange} /> ))} </div> ); } } 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>
Since it is an array containing the state of multiple checkboxes, you need to tell handleChange
which checkbox you want to change: 由于它是一个包含多个复选框状态的数组,因此需要告诉
handleChange
您要更改哪个复选框:
<Checkbox
key={i}
onChange={(e) => this.handleChange(e, i)}
name={list.id}
value="Yes"
label={description}
/>
You must then create a temporary copy of the state array, mutate the appropriate item, then replace the old array with the new one: 然后,您必须创建状态数组的临时副本,更改适当的项,然后用新的替换旧的数组:
handleChange(e, i) {
const arr = this.state.listid.slice();
arr[i] = e.target.name;
this.setState({listid: arr});
}
A better approach though would be to pass the index i
as a prop down to Checkbox
and then do: this.props.onClick(this.props.index)
or similar. 不过,更好的方法是将索引
i
作为道具传递到Checkbox
,然后执行: this.props.onClick(this.props.index)
或类似的东西。
Ah, classic. 啊,经典。 Here are the changes you can make to
renderCheckBox
. 这是您可以对
renderCheckBox
进行的更改。
<Checkbox
key={i}
onChange={(el) => this.handleChange(el, i)}
name={list.id}
value="Yes"
label={description}
/>
In your handleChange
, you can then do 在您的
handleChange
,您可以执行
handleChange(el, index) {
let listid = [ ...this.state.listid ];
listid[index] = el.target.name;
this.setState({
listid,
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.