无法在动态生成的组件上设置state()

时间:2017-02-21 00:22:57

标签: reactjs meteor

让keyComponent 过滤关键字的字符串,并使用事件处理程序返回它们(以切换它们)并在 this.state 中生成状态(targetState)。问题是,如果我点击任何关键字,状态不会更新/更改。我可以通过console.log看到在this.state中生成的所有状态。它们在点击时根本没有更新,也没有错误。

我会感谢一些帮助;-)

import React from 'react';
import { render } from 'react-dom';
import { sectionUpsert } from '/imports/api/userProgress/upsertMethods.jsx';


export default class LetsCheck extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            reference: props.reference,
            text: props.text,
            keys: props.keys,
            CorrectArray: [],

        };
    }

    handleClick(e, TxtBit, targetState) {
        console.log(targetState);
        console.log(this.state.targetState);
        let tempA = this.state.CorrectArray;
        let target = targetState;
        tempA.push(TxtBit);
        let obj = { [target]: true, }
        console.log(obj);
        this.setState(obj);

        // this.setState({
        //  CorrectArray: tempA,
        //  [target]: true,
        // });

        console.log(this.state);

    }

    handleUnclick(e, TxtBit, targetState) {
        console.log('unclicked' + TxtBit + index);
    }

    componentWillUnmount() {
        let keys = this.state.keys;
        let correct = this.state.CorrectArray;
        let keyWW = keys.filter(function(key){
            return !correct.includes(key) && keys.indexOf(key) % 2 === 0 
        });
        const secData = {
            userId: Meteor.userId(),
            ref: this.state.reference,
            keyWR: this.state.CorrectArray,
            keyWW: keyWW,
            porSect: Math.round((this.state.CorrectArray.length / (keyWW.length + this.state.CorrectArray.length)) * 100),
        };
        sectionUpsert.call(secData);
    }

    render() {
        let keys = this.state.keys;
        let clicked = this.state;
        let filter = keys.filter( function(key) {
            return keys.indexOf(key) % 2 === 0; 
        });
        let KeyComponent = this.state.text.map(function(TxtBit, index) {
            let match = false;
            let checkMatch = function(TxtBit, filter) {
                for (var y = filter.length - 1; y >= 0; y--) {
                    if ( TxtBit == filter[y] ) {
                        match = true 
                    }
                }
            };
            checkMatch(TxtBit, filter);
            if( match ) {
                targetState = 'KeyBtn' + index; 
                clicked[targetState] = false;
            return <a href="#" key={ index } style={{ display: `inline` }} onClick={ this.state[index] ? () => this.handleUnclick(this, TxtBit, targetState) : () => this.handleClick(this, TxtBit, targetState) } name={ TxtBit } className={ this.state[index] ? 'clicked': 'unclicked' } > { " "+TxtBit+ " " }</a>;
            } else {
                return <div  key={ index } style={{ display: `inline` }} className="TxtBit"> { " "+TxtBit+ " " }</div>;
            }
        }.bind(this)); 
        console.log(this.state);
        return(
            <div className="content">
              <div className="content-padded">
                    <div> {KeyComponent}</div>
                    <p> { this.state.CorrectArray.length } / {  this.state.keys.length / 2 } </p>


                </div>
            </div>
            );
    }
};

1 个答案:

答案 0 :(得分:2)

尝试绑定它们: this.handleUnclick(this,TxtBit,targetState).bind(this)

或在处理程序上使用箭头功能...... 例如:https://blog.josequinto.com/2016/12/07/react-use-es6-arrow-functions-in-classes-to-avoid-binding-your-methods-with-the-current-this-object/

问候!

相关问题