gpt4 book ai didi

javascript - 在 React.js 中将 key 传递给 child

转载 作者:可可西里 更新时间:2023-11-01 01:45:23 25 4
gpt4 key购买 nike

我正在浏览一个关于 tutsplus 的 React 教程,它有点旧,而且代码不能像最初编写的那样工作。实际上,我完全同意这一点,因为它迫使我更加独立地学习,但是我花了一段时间来解决一个我无法弄清楚的错误。该错误包括无法传递对象键,这会阻止我的程序更新正确对象的状态。

如果您想运行此代码并查看它的运行情况,首先是存储库:https://github.com/camerow/react-voteit

我有一个看起来像这样的子组件:

var FeedItem = React.createClass({

vote: function(newCount) {
console.log("Voting on: ", this.props, " which should have a key associated.");

this.props.onVote({
key: this.props.key,
title: this.props.title,
description: this.props.desc,
voteCount: newCount
});
},

voteUp: function() {
var count = parseInt(this.props.voteCount, 10);
var newCount = count + 1;
this.vote(newCount);
},

voteDown: function() {
var count = parseInt(this.props.voteCount, 10);
var newCount = count - 1;
this.vote(newCount);
},

render: function() {
var positiveNegativeClassName = this.props.voteCount >= 0 ?
'badge badge-success' :
'badge badge-danger';
return (
<li key={this.props.key} className="list-group-item">
<span className={positiveNegativeClassName}>{this.props.voteCount}</span>
<h4>{this.props.title}</h4>
<span>{this.props.desc}</span>
<span className="pull-right">
<button id="up" className="btn btn-sm btn-primary" onClick={this.voteUp}>&uarr;</button>
<button id="down" className="btn btn-sm btn-primary" onClick={this.voteDown}>&darr;</button>
</span>
</li>
);
}

});

现在,当有人点击投票按钮时,所需的行为是 FeedItem.vote() 方法将对象发送到主 Feed 组件:

var FeedList = React.createClass({

render: function() {
var feedItems = this.props.items;

return (
<div className="container">
<ul className="list-group">

{feedItems.map(function(item) {
return <FeedItem key={item.key}
title={item.title}
desc={item.description}
voteCount={item.voteCount}
onVote={this.props.onVote} />
}.bind(this))}
</ul>
</div>
);
}

});

应该通过父组件的 onVote 函数传递该键:

var Feed = React.createClass({

getInitialState: function () {
var FEED_ITEMS = [
{
key: 1,
title: 'JavaScript is fun',
description: 'Lexical scoping FTW',
voteCount: 34
}, {
key: 2,
title: 'Realtime data!',
description: 'Firebase is cool',
voteCount: 49
}, {
key: 3,
title: 'Coffee makes you awake',
description: 'Drink responsibly',
voteCount: 15
}
];
return {
items: FEED_ITEMS,
formDisplayed: false
}
},

onToggleForm: function () {
this.setState({
formDisplayed: !this.state.formDisplayed
});
},

onNewItem: function (newItem) {
var newItems = this.state.items.concat([newItem]);
// console.log("Creating these items: ", newItems);
this.setState({
items: newItems,
formDisplayed: false,
key: this.state.items.length
});
},

onVote: function (newItem) {
// console.log(item);

var items = _.uniq(this.state.items);
var index = _.findIndex(items, function (feedItems) {
// Not getting the correct index.
console.log("Does ", feedItems.key, " === ", newItem.key, "?");
return feedItems.key === newItem.key;
});
var oldObj = items[index];
var newItems = _.pull(items, oldObj);
var newItems = this.state.items.concat([newItem]);
// newItems.push(item);
this.setState({
items: newItems
});
},

render: function () {
return (
<div>
<div className="container">
<ShowAddButton displayed={this.state.formDisplayed} onToggleForm={this.onToggleForm}/>
</div>
<FeedForm displayed={this.state.formDisplayed} onNewItem={this.onNewItem}/>
<br />
<br />
<FeedList items={this.state.items} onVote={this.onVote}/>
</div>
);
}

});

我的逻辑依赖于能够协调 onVote 函数中的键,但是键 Prop 没有被正确传递。所以我的问题是,我如何通过这种“单向流”将它们传递给我的父组件?

注意:请随意指出其他问题或更好的设计决策,或绝对的愚蠢。甚至我问错了问题。

期待对这个很酷的框架进行很好的探索。

最佳答案

The key prop has a special meaning in React .它不会作为 prop 传递给组件,但会被 React 用来帮助协调集合。如果你知道d3 ,它的工作原理类似于 selection.data() 的关键功能。它允许 React 将前一棵树的元素与下一棵树的元素相关联。

你有一个 key 很好(如果你传递一个元素数组,你需要一个),但是如果你想把这个值传递给组件,你应该有另一个属性:

<FeedItem key={item.key} id={item.key} ... />

(并在组件内访问 this.props.id)。

关于javascript - 在 React.js 中将 key 传递给 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30465651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com