Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 当我们总是有一个大小为1的数组时,如何为组件分配键_Reactjs - Fatal编程技术网

Reactjs 当我们总是有一个大小为1的数组时,如何为组件分配键

Reactjs 当我们总是有一个大小为1的数组时,如何为组件分配键,reactjs,Reactjs,我有一个reactjs组件,我使用它如下: render() { var returnVar=""; returnVar= <div> { this.props.channels.preferedChannelsList.map(function (item, i) { return ( <StoryBoard

我有一个reactjs组件,我使用它如下:

   render() {
    var returnVar="";
            returnVar= <div>
                {
                    this.props.channels.preferedChannelsList.map(function (item, i) {
                        return ( <StoryBoard
                            key={this.props.match.params.search === "search"?Math.random():"storyBoard-" + i + "-" + item.name.replace("&","and").replace("`","").replace("'","")}
                         ></StoryBoard>)
                    }.bind(this))

            </div>;
        </div>

    return (
        returnVar

    );
  }
}
正如您所见,我必须根据路径为组件情节提要选择不同的键。原因是在搜索的情况下,我总是有一个大小为1的数组,如果我在执行不同的搜索时选择I作为索引,序列图像板组件不会渲染,因为它不理解该组件已根据键进行了更改。我不确定分配随机值是否是一个好方法。在这种情况下,有没有更好的方法来分配密钥

我不确定分配随机值是否是一个好方法

密钥应该是稳定的、可预测的和唯一的。不稳定的键(如Math.random生成的键)将导致不必要地重新创建许多组件实例和DOM节点,这可能会导致性能下降和子组件中的状态丢失

更多

最好使用项目中的一些散列值

this.props.channels.preferedChannelsList.map(function (item, i) {
                        return ( <StoryBoard
                            key={item.someKeyWithUniqueData}
                         ></StoryBoard>)
                    }.bind(this))