[英]Updating state from child component to parent component
I'm struggling with an issue of App.js which has a Checkboxes component.我正在努力解决具有 Checkboxes 组件的 App.js 问题。 When someone ticks a checkbox in that component, I want the state for that checkbox to be updated in the App.js file.当有人勾选该组件中的复选框时,我希望在 App.js 文件中更新该复选框的 state。 The code for my Checkboxes component is below.我的 Checkboxes 组件的代码如下。 I am using material ui if that makes any difference.如果这有什么不同,我正在使用材料 ui。
import Checkbox from '@material-ui/core/Checkbox';
import FormGroup from '@material-ui/core/FormGroup';
import FormControlLabel from '@material-ui/core/FormControlLabel';
export default function CheckboxLabels() {
return (
<FormGroup row>
<FormControlLabel
control={<Checkbox name="checkedD" />}
label="Remove Duplicates"
/>
</FormGroup>
);
}
The code in App.js that is relevant is: App.js 中相关的代码是:
<Grid>
<Checkboxes />
</Grid>
What should I be doing?我应该做什么? How do I modify the above code?如何修改上面的代码?
If you want to keep the state in the App
component, then you need to pass a function to your child component which will be used to send the state to the parent ( App
) component.如果您想将 state 保留在App
组件中,则需要将 function 传递给您的子组件,该子组件将用于将App
发送给父组件。
In your parent component, you would create the state so that each state of checkboxes can be stored and pass the function that will handle the updating of the state. In your parent component, you would create the state so that each state of checkboxes can be stored and pass the function that will handle the updating of the state. I would store the state in Map
to avoid having duplications of the same checkbox on each update.我会将 state 存储在Map
中,以避免在每次更新时重复使用相同的复选框。 Also, pass an id
to each of the Checkbox components so that you know which state refers to which checkbox when updating later on.此外,将id
传递给每个 Checkbox 组件,以便您在以后更新时知道哪个 state 指的是哪个复选框。
import React from "react";
import CheckboxLabels from "./CheckboxLabel";
class App extends React.Component {
state = {
checkboxes: new Map()
};
handleClick = (e, id) => {
const { checked } = e.target;
this.setState((prevState) => ({
checkboxes: prevState.checkboxes.set(id, checked)
}));
};
render() {
return (
<div className="App">
<CheckboxLabels id={1} handleClick={this.handleClick} />
<CheckboxLabels id={2} handleClick={this.handleClick} />
<CheckboxLabels id={3} handleClick={this.handleClick} />
<CheckboxLabels id={4} handleClick={this.handleClick} />
</div>
);
}
}
export default App;
In your child component, you would accept the function and the id
and then pass that function with the event
and id
in onChange
method of the checkbox.在您的子组件中,您将接受 function 和id
,然后在复选框的onChange
方法中使用event
和id
传递 function。
import React from "react";
import Checkbox from "@material-ui/core/Checkbox";
import FormGroup from "@material-ui/core/FormGroup";
import FormControlLabel from "@material-ui/core/FormControlLabel";
export default function CheckboxLabels({ id, handleClick }) {
return (
<FormGroup row>
<FormControlLabel
control={<Checkbox name="checkedD" />}
label="Remove Duplicates"
onChange={(e) => handleClick(e, id)}
/>
</FormGroup>
);
}
This way, you can store the state of multiple checkboxes and you can easily track which state is which by an id
.这样,您可以存储多个复选框的 state 并且您可以通过id
轻松跟踪哪个 state 是哪个。
You can use regular state mechanism in parent and just pass it down to CheckboxLabels
component.您可以在父级中使用常规的 state 机制,然后将其传递给CheckboxLabels
组件。 So send setChecked
and checked
values down:所以向下发送setChecked
和checked
值:
export default function CheckboxLabels({ checked, onChange }) {
return (
<FormGroup row>
<FormControlLabel
control={
<Checkbox
name="checkedD"
checked={checked}
onChange={(e) => {
if (onChange) {
onChange(e.target.checked);
}
}}
/>
}
label="Remove Duplicates"
/>
</FormGroup>
);
}
export default function App() {
const [checked, setChecked] = useState(false);
return (
<Grid>
<CheckboxLabels checked={checked} onChange={setChecked} />
</Grid>
);
}
I am not sure if your code is good naming-wise, but I added that checkbox component is name CheckboxLabels
我不确定您的代码在命名方面是否良好,但我添加了该复选框组件的名称为CheckboxLabels
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.