简体   繁体   English

当使用新道具调用渲染时,React JS不会完全更新视图

[英]React JS Not Completely Updating View When Render Is Called With New Props

In React JS I have an array that I am passing as a prop (I do not want to use state). 在React JS中我有一个数组,我作为道具传递(我不想使用状态)。 If I remove an item from the array and rerender the component, the fields update in number, but do not update in value. 如果我从数组中删除项目并重新呈现组件,则字段会更新,但不会更新值。 The prop being passed is updated. 传递的道具已更新。 I don't want the textarea value set, because I do not want to use state for this component, so the prop is a child of textarea This is not my actual component, but something I created to demonstrate the problem. 我不想设置textarea值,因为我不想对这个组件使用state,所以prop是textarea的子节点。这不是我的实际组件,而是我创建的用于演示问题的东西。 My understanding is that when props update, so should the view. 我的理解是,当道具更新时,视图也应如此。

Here is a JSFiddle: https://jsfiddle.net/69z2wepo/12745/ 这是一个JSFiddle: https ://jsfiddle.net/69z2wepo/12745/

items=["One","Two","Three","Four","Five"];


var Areas=React.createClass({
        handleClickAdd: function() {
        items.push("");
        rerender();
    },

    handleClickRemove: function(fieldindex,event) {
        items.splice(fieldindex,1);
        rerender();
    },

    handleKeyUp: function(event) {
        sdbobj.resizeArea(event.target);
    },

    render: function() {
        return (
            <div>
                {this.props.items.map(function(item,i,arr){
                    return (
                        <div key={i}>
                            <textarea  onKeyUp={this.handleKeyUp}>
                                {item}
                            </textarea>

                            {(arr.length>1?<button onClick={this.handleClickRemove.bind(this,i)}>Remove</button>:"")}
                        </div>
                    )
                }.bind(this)) }
                <button onClick={this.handleClickAdd}>
                    Add
                </button>
                <div>
                    Items = [{items.toString()}]
                </div>
            </div>
        )
    }
});

function resizeArea(elem)
{
    elem.style.height="1px";
    elem.style.height=(elem.scrollHeight)+"px";
}

function rerender()
{
    React.render(<Areas items={items} />, document.getElementById('container'));
}

React.render(<Areas items={items} />, document.getElementById('container'));

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanks 谢谢

Checkout React's article on Reconciliation ... since the key is the index, it becomes out of sync after you remove the item and React can't see that it's changed. Checkout React关于Reconciliation的文章 ...由于密钥是索引,因此在删除项目后它会变得不同步而React无法看到它已被更改。 If you make the key bound to either the item text or an item id, it should work. 如果您将键绑定到项目文本或项目ID,它应该工作。

 <div key={item}> <textarea onKeyUp={this.handleKeyUp}> {item} </textarea> {(arr.length>1?<button onClick={this.handleClickRemove.bind(this,i)}>Remove</button>:"")} </div> 

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

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