Reactjs “控制台说我的状态”;参赛作品;没有定义吗?你能帮助我,如何在React.js中定义状态吗?

Reactjs “控制台说我的状态”;参赛作品;没有定义吗?你能帮助我,如何在React.js中定义状态吗?,reactjs,Reactjs,在这里,我创建了TodoItems组件,并尝试将数组存储为状态,但console说我的条目未定义: var TodoItems = React.createClass({ getInitialState: function() { return { entries: this.props.entries }; }, removeItem: function(key){ v

在这里,我创建了TodoItems组件,并尝试将数组存储为状态,但console说我的条目未定义:

var TodoItems = React.createClass({
        getInitialState: function() {
          return {
            entries: this.props.entries
          };
        },

      removeItem: function(key){
        var itemArray = this.state.entries;
        for (var i = 0; i < itemArray.length; i++)
          if (itemArray[i.key] === key) {
              itemArray.splice(i, 1);
              break;
          }
          this.setState({
            entries: entries
          })
      },
var TodoItems=React.createClass({
getInitialState:函数(){
返回{
条目:this.props.entries
};
},
removeItem:功能(键){
var itemArray=this.state.entries;
对于(var i=0;i
这是我试图存储的道具:

</div>
    <TodoItems entries={this.state.items}/>
</div>

您犯了一个错误,在removeItem函数中将状态设置为
entries
,您应该将其设置为
itemArray
,同时,将itemArray分配为
var itemArray=[…this.state.entries];
因为在您的声明风格中,它将引用状态条目本身,并将直接改变状态,这是不可取的,并且只在TodoItems的
第一次呈现时调用
getInitialState
,因此,如果要使用props分配状态,请确保在
组件WillReceiveprops
中执行此操作函数,因为每次父级渲染时都会调用它,并且会更新道具

var TodoItems = React.createClass({
        getInitialState: function() {
          return {
            entries: this.props.entries
          };
        },
        componentWillReceiveProps(nextProps) {
             this.setState({entries: nextProps.entries})
        }
      removeItem: function(key){
        var itemArray = [...this.state.entries];
        for (var i = 0; i < itemArray.length; i++)
          if (itemArray[i.key] === key) {
              itemArray.splice(i, 1);
              break;
          }
          this.setState({
            entries: itemArray
          })
      },
var TodoItems=React.createClass({
getInitialState:函数(){
返回{
条目:this.props.entries
};
},
组件将接收道具(下一步){
this.setState({entries:nextrops.entries})
}
removeItem:功能(键){
var itemArray=[…this.state.entries];
对于(var i=0;i
removietem
中的
条目是什么?
没有定义。更正了额外的代码缩进和大写。好吧,现在我又出现了一个错误,但旧的错误似乎消失了,谢谢!新的错误使整个本地主机崩溃,并说“在现有状态转换期间,setstate无法更新”。你能帮我吗?是的,但这取决于你如何使用其余的代码,这在你的问题中没有提到。所以我想你可以关闭这个并问一个新的