[英]populate the checkbox dynamically and maintain state of checkbox in react redux
我们已经通过调用api服务动态填充了复选框项。 根据集合,我们将显示带有选中状态的复选框。
如果我选中/取消选中复选框,如何为动态填充的复选框项实现handlechange事件。
我想获取当前的复选框状态。 我怎么才能得到它
在checkboxcontainer.tsx中
data.map(item => (
<label key={item.projectId} className="checkBoxcontainer">
<CheckBox name={item.projectName}
checked={item.checked} handleChange={this.handleChange} />
{item.projectName}<span className="checkmark"/>
</label>
Redux:
const mapStateToProps=({projects} : ApplicationState) => ({
data: projects.data
});
const mapDispatchToProps = (dispatch: Dispatch) => ({
fetchProjects: bindActionCreators(fetchProjects, dispatch),
});
const ProjectListContainer = connect(mapStateToProps,mapDispatchToProps)(ProjectContainer);
export { ProjectListContainer as ProjectContainer };
手柄更换功能:
handleChange(e :any) {
// how to maintain the checkbox state
}
import * as React from 'react';
interface IPropTypes {
type?: string,
name: string,
checked?: boolean,
handleChange(event: any): void;
}
class CheckBox extends React.Component<IPropTypes,{}> {
render() {
return (
<input type='checkbox' name={this.props.name} checked={this.props.checked}
onChange={ e => this.props.handleChange(e) } />
);
}
}
我建议传递给Checkbox
的handleChange函数如下所示:
handleChange={(e) => this.handleChange(e, item.projectId)}
然后在handleChange
您可以获取是否选中了复选框,然后使用projectId
和复选框状态触发动作
const isChecked = event.target.checked; someAction(projectId, isChecked);
此操作应更改您的Redux存储中的数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.