简体   繁体   English

React复选框无法正常工作

[英]React checkbox doesn't work as expected

I had and idea to ad some object style to React project. 我有想法向React项目添加一些对象样式。 That is why I have written that class 这就是为什么我写那堂课

 export class Tagi {
 constructor() {
    this.members = [{
        tag: "No matter",
        color: "Aquamarine",
        isMarked: true
    }];
   this.handleTagMarking = this.handleTagMarking.bind(this);
}

add(x) {
    this.members.push(x);
}
static fromTable(table) {
    let tags = new Tagi();
    let shortened = unique(table);
    for (let value of shortened) {
        let record = {
            tag: value,
            color: colors[shortened.indexOf(value)],
            isMarked: false
        }
        tags.add(record)
    }
return tags;

}

get getAllTags() {
    return this.members;
}

handleTagMarking(event){

let x = event.target.value;
const index = this.members.findIndex((element)=>element.tag==x);
const currentMarkStatus = this.members[index].isMarked;
if (currentMarkStatus) this.UnMarkTag(index); else this.MarkTag(index)
console.log(this.members)

}

The last part thereof is event handler, more about it later. 它的最后一部分是事件处理程序,稍后会详细介绍。 That class is implemented in Parent component like this 此类在Parent组件中实现,如下所示

let Taggs =Tagi.fromTable(d);
console.log (Taggs.getMarkedTags);

Please note that this is implemented in its render function. 请注意,这是在其渲染功能中实现的。 It has nothing to do with its state. 它与状态无关。 Later in this Parent component I send it as props 稍后在此父组件中,我将其作为道具发送

render() {
const label = this.props.labels;
const SingleCheckbox =()=>{return(label.map((element)=>
<label key={element.tag} ><input type="checkbox" value={element.tag}   checked={element.isMarked} onChange={this.props.fn} />{element.tag}</label>))}


return (
<div className="checkbox">
 <SingleCheckbox />
 </div>);

The problem is that checkbox doesn't react to checking items properly. 问题在于复选框不能正确地检查项目。 What I mean by this is that data is changed ( I send to console the data within evenhandler so that is clear) but it does not check the fields what is expected behaviour. 我的意思是更改数据(我将数据发送到evenhandler中以进行控制台操作,这样很清楚),但是它不会检查字段的预期行为。 I suspect it happens because in Parent, Taggs are not state-linked (but only suspect) Is that correct or could there be other reason? 我怀疑这是因为在Parent中,Taggs与状态无关(只是怀疑),这是正确的还是其他原因? If so, how could I easily reshape the code keeping its object oriented style? 如果是这样,我如何轻松地重塑代码以保持其面向对象的风格?

I have just chcecked my initial idea of being-not-a-state. 我刚刚开始了关于“不是国家”的最初想法。 That is not correct I guess. 我猜那是不正确的。 Now in constructor Taggs are initiated like this: 现在在构造函数中,Tagg的启动方式如下:

Taggs:Tagi.fromTable(props.disciplines.map((d) => d.tags)),

and later in render{return()} are called like this 然后在render {return()}中这样调用

<CheckBox labels ={this.state.Taggs.getAllTags} fn={this.state.Taggs.handleTagMarking}/>

Does not change anything at all 完全不改变任何东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM