简体   繁体   English

React中状态更改后的渲染组件

[英]Rerender Component upon state change in React

I have an app which calls two functions; 我有一个可以调用两个功能的应用程序: MyCart and MyItems. MyCart和MyItems。 I am trying to make it so when the button rendered in MyItems is called it calls a method inside the main app component (the parent) and changes the state but for some reason, it doesn't change the value in the MyCart span. 我试图做到这一点,以便在调用MyItems中呈现的按钮时,它调用主应用程序组件(父级)内部的方法并更改状态,但由于某些原因,它不会更改MyCart跨度中的值。

My code below is what I am trying to do but it's not working and I can't figure out why. 我下面的代码是我要尝试执行的操作,但是它不起作用,我不知道为什么。

class ShopRemake extends React.Component {
    constructor() {
        super();
        this.state = {
            total: 0
        }

        this.changeTotal = this.changeTotal.bind(this);
    }

    changeTotal() {
        this.setState(prevState => {total: ++prevState.total});
        console.log(this.state);
    }

    render() {
        return (
            <React.Fragment>
                <MyItem changeTotal={this.changeTotal}/>
                <MyCart total={this.state.total}/>
            </React.Fragment>
        );
    }
}

function MyItem(props) {
    return (
        <button onClick={props.changeTotal}> Click Me </button>
    );
}

function MyCart(props) {
    return (
        <span> Total Items: {props.total} </span>
    );
}

I want it so that after I click the button the span inside the MyCart to update accordingly. 我想要它,以便在单击按钮后MyCart内的跨度相应地更新。

You must wrap the returning object literal into parentheses in your arrow functions. 您必须将返回的对象文字包装在箭头函数中的括号内。 Otherwise curly braces will be considered to denote the function's body. 否则,花括号将被视为表示功能的主体。 The following works: 以下作品:

p => ({ foo: 'bar' })

So the call to setState in changeTotal : 因此,在changeTotal调用setState

changeTotal() {
    this.setState(prevState => {total: ++prevState.total});
    console.log(this.state);
}

Should be replaced by the following : 应替换为以下内容:

changeTotal() {
    this.setState(prevState => ({total: ++prevState.total}));
    console.log(this.state);
}

Also, setState is asynchronous, you will need to either await it(not recommended) or use the callback as the second parameter to see the resulting state : 同样, setState是异步的,您将需要等待它(不推荐)或使用回调作为第二个参数来查看结果状态:

changeTotal() {
    this.setState(
        prevState => ({total: ++prevState.total}),
        () => { console.log(this.state) }
    );
}

EDIT : 编辑:

Sidenote, the 2 components below in your code can be reduced into arrow functions for the exact same result, while making them easier to read : 旁注,可以将代码中下面的2个组件简化为箭头函数,以得到完全相同的结果,同时使它们更易于阅读:

const MyItem = ({ changeTotal }) => <button onClick={changeTotal}> Click Me </button>

const MyCart = ({ total }) => <span> Total Items: {total} </span>

And you can also convert changeTotal to an arrow function to avoid having to bind it in your constructor 您还可以将changeTotal转换为箭头函数,以避免必须将其绑定到构造函数中

Your setState is not returning an object here. 您的setState不在此处返回对象。 Try to change your method changeTotal to this 尝试将您的方法changeTotal更改为此

changeTotal() {
     this.setState(prevState => ({ total: prevState.total + 1 }), () => 
     console.log(this.state));
}

The setState methods is asynchronous, if you want to log it's value, do this with a callback. setState方法是异步的,如果要记录其值,请使用回调进行此操作。

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

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