简体   繁体   English

React - State 在一个父 function 中定义,但不在另一个父 function 中被子调用

[英]React - State is defined in one parent function, but not in the other parent function being called from the child

I'm re-learning React, and trying to build a simple TODO app, that you can add and remove items from.我正在重新学习 React,并尝试构建一个简单的 TODO 应用程序,您可以从中添加和删除项目。

My problem is when calling handleRemove() .我的问题是在调用handleRemove()时。 Both functions are in my parent component, and I'm able to pass down handleRemove() to my child component.这两个函数都在我的父组件中,我可以将handleRemove()传递给我的子组件。 But when I try to setState in handleRemove, it comes up as undefined.但是当我尝试在 handleRemove 中设置状态时,它出现为未定义。 I'm not sure why, seeing that it is almost the same as handleAdd()我不知道为什么,看到它与handleAdd()几乎相同

This is my Todo:这是我的待办事项:

import React from 'react';
import List from './List';

export default class Todo extends React.Component{

        state= {
            listItems: [],
            count: 0
        }    

    
    handleAdd = (itemToAdd) =>{
        document.querySelector("#input").textContent="";


        this.setState(prevState => ({
            listItems: [...prevState.listItems, itemToAdd],
            count: prevState.count+1
        }))
    }


    handleRemove = (itemToRemove) =>{
        

        let newListItems = this.state.listItems;
        let indexOfRemove = newListItems.indexOf(itemToRemove);
        newListItems.splice(indexOfRemove, 1);


        console.log(newListItems);

        //Setting listItems to the newly created newListItems
        this.setState(prevState => ({
            listItems: newListItems,
            count: prevState.count-1
        }))
    }

    render(){
        return(
            <div>
                <p>Number of items: {this.state.count}</p>
                <input type="text" id="input"/>
                <button onClick={() => this.handleAdd(document.querySelector("#input").value)}>Add</button> 
                <List removeHandle={this.handleRemove} items={this.state.listItems}/>
            </div>
        )
    }
}

This is my List:这是我的清单:

import React from 'react';

function List(props){
        console.log(props); // props all show up as they're supposed to here
        
        return(
            <ol>
                {
                    props.items.map((item, index) => {
                        return(
                            <div key={index}>
                                <li key={index}>{item}</li>
                                <button onClick={() => props.removeHandle(item)}>Remove from list</button>
                            </div>
                            
                        );
                    })
                }
            </ol>
        )
    }
export default List;

I feel like this is something obvious, but I've looked and can't find any clear answer我觉得这很明显,但我看过但找不到任何明确的答案

Alright, just found the solution 5 minutes after posting this, classic .好的,发布后5分钟才找到解决方案,经典

I'll keep this up for any beginners who are confused when trying to keep all state logic in the correct place.对于在尝试将所有 state 逻辑保持在正确位置时感到困惑的任何初学者,我会继续这样做。

The problem is that when passing down the handleRemove() to my List , I needed to add.问题是当将handleRemove()传递给我的List时,我需要添加。 bind(this) , like such: bind(this) ,像这样:

<List removeHandle={this.handleRemove.bind(this)} items={this.state.listItems}/>

Not exactly sure why it's needed, but i'll be looking into it.不完全确定为什么需要它,但我会调查它。

If somebody knows why .bind() is needed, feel free to leave a comment如果有人知道为什么需要.bind() ,请随时发表评论

Update: See above comment更新:见上面的评论

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

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