我正在运行有关tutsplus的反应教程,这个教程有点旧,而且代码不像最初编写的那样工作.我实际上对此完全没问题,因为它迫使我更加独立地学习,但是我花了一些时间来处理一个我无法弄清楚的错误.该错误包括无法传递对象密钥,这会阻止我的程序更新正确对象的状态.
首先,如果你想运行这个代码并在运行中看到它,那么就是repo: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' : …Run Code Online (Sandbox Code Playgroud)