简体   繁体   English

为什么组件不能提供正确的信息? ReactJS

[英]Why isn't the component rendering the correct information? ReactJS

I'm rendering an array of values and I have a two buttons, one to Add and another to Remove . 我正在渲染一个值数组,我有两个按钮,一个按钮为Add ,另一个按钮为Remove Everything works the way it should but when I click Remove , the element in the correct index gets removed from the array but when it renders again, it removes the last entry, and not the entry at that specific index. 一切正常,但是当我单击Remove ,正确索引中的元素将从数组中删除,但是当再次呈现时,它将删除最后一个条目,而不是该特定索引处的条目。

For example: (left side is webpage, right side is console) 例如:( 左侧是网页,右侧是控制台)

在此处输入图片说明

Currently the array is ["hello", "world", "everyone"] and let's say I want to remove "world" and I click the Remove button next to it, the array updates correctly (I did a console.log) and it becomes ["hello", "everyone"] , it re-renders but it shows it as if the array was ["hello", "world"] and renders it incorrectly: 当前该数组为["hello", "world", "everyone"] ,假设我要删除"world" ,然后单击它旁边的“ Remove按钮,该数组会正确更新(我做了console.log),并且它变成["hello", "everyone"] ,它重新渲染,但显示为好像数组是["hello", "world"]并错误地呈现它:

在此处输入图片说明

I'm not sure what I did incorrectly with my code as the array gets updated correctly and everything else seems to be okay. 我不确定我对代码所做的错误操作,因为该数组已正确更新,并且其他一切似乎都还可以。

    childChange: function(name, valid, value) {

        this.state.values = this.props.values;
        var pattern = /[0-9]+/;                                             // Using regex to find last digits from 0-9
        var match = name.match(pattern);                                    // Update state
        var i = parseInt(match);                                            // Parse char into int
        // console.log(match);

        this.state.values[i] = value;

        this.setState(this.state);

        this.props.callback(                                                // Call parent callback
            this.props.name,
            this.props.node.valid(this.state.values),
            this.state.values
        );
    },

    addEmptyItem: function() {

        this.state.values = this.props.values;
        this.state.values.push("");
        this.setState(this.state);

    },

    removeItem: function(ev) {

        console.log(ev.currentTarget.dataset.index);
        var i = parseInt(ev.currentTarget.dataset.index);
        this.state.values = this.props.values;
        this.state.values.splice(i,1);
        console.log(this.state.values);
        this.setState(this.state.values);

    },

        render: function() {
            var that = this;

            console.log("===RENDER===");

            return (
                <div id = "form">
                {this.props.values.map(function(v, i) {
                    return (

                        <div>
                            {(that.props.node.get().constructor.name === "Parent") ?
                            <ParentComponent
                                name={that.props.name + i}
                                key={i}
                                timer={that.props.timer}
                                callback={that.childChange}
                                values={v}
                                newParent={that.props.node.get()}
                            />
                            :
                            <div>
                                <NodeComponent
                                    name={that.props.name + i}
                                    key={i}
                                    timer={that.props.timer}
                                    callback={that.childChange}
                                    value={v}
                                    newNode={that.props.node.get()}
                                />

                            </div>

                            }
                            <button data-index={i} onClick={that.removeItem}>Remove
                            </button>
                        </div>

                    )
                })}
                <button onClick={() => that.addEmptyItem()}>Add
                </button>

                </div>
           )

        }

Change your function to look like this: 将函数更改为如下所示:

removeItem: function(ev) {

        console.log(ev.currentTarget.dataset.index);
        var i = parseInt(ev.currentTarget.dataset.index);
        let values = [...this.props.values];
        values.splice(i,1);
        console.log(this.state.values);
        this.setState(values);

    }

Why? 为什么? when you use react state, you never changing it directly. 当您使用反应状态时,您永远不会直接更改它。 you might look about it in this article . 您可能会在本文对此有所了解。 what you did here, is you changed the state and then you called 'setState', that doesn't work. 您在这里所做的是先更改状态,然后再调用“ setState”,但这是行不通的。 you should create new variable, manipulate over the new variable, and then call the setState with the new variable 您应该创建新变量,对新变量进行操作,然后使用新变量调用setState

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

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