Javascript 动态填充复选框并在react redux中保持复选框的状态
通过fetch调用api服务,我们动态填充了复选框项。根据集合,我们将显示选中状态的复选框 如果我选中/取消选中复选框,如何为动态填充的复选框项实现handlechange事件 我想获取当前复选框状态。我怎样才能得到它 在checkboxcontainer.tsx中Javascript 动态填充复选框并在react redux中保持复选框的状态,javascript,reactjs,Javascript,Reactjs,通过fetch调用api服务,我们动态填充了复选框项。根据集合,我们将显示选中状态的复选框 如果我选中/取消选中复选框,如何为动态填充的复选框项实现handlechange事件 我想获取当前复选框状态。我怎样才能得到它 在checkboxcontainer.tsx中 data.map(item => ( <label key={item.projectId} className="checkBoxcontainer">
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>
手柄更改功能:
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) } />
);
}
}
handleChange(e:any){
//如何维护复选框状态
}
从“React”导入*作为React;
接口IPropTypes{
类型?:字符串,
名称:string,
选中?:布尔,
handleChange(事件:任何):无效;
}
类复选框扩展了React.Component{
render(){
返回(
this.props.handleChange(e)}/>
);
}
}
我建议传递给复选框的handleChange函数如下所示:
handleChange={(e)=>this.handleChange(e,item.projectId)}
然后在handleChange
中,您可以抓取复选框是否选中,然后使用projectId
和复选框的状态触发操作
const isChecked=event.target.checked;
一些动作(projectd,已检查);
此操作应更改redux存储中的数据您是否可以添加更多代码来说明如何触发某个操作。。我不清楚这一点,我的意思是触发一个新操作,与获取数据并将其添加到存储中的操作相同,但在这种情况下,您不获取数据,您将只触发新操作,处理项目的减速机应根据新操作进行操作并进行更改
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) } />
);
}
}