何时/何地在React中设置新状态

时间:2018-05-29 18:35:40

标签: reactjs redux react-redux

在我的App.js文件中,我获取所需的数据并将值分配给不同的商店状态。

constructor(props){
        super(props);
        this.props.fetchUser();
        this.props.fetchBeers();


    }

现在在子组件中,我尝试从商店中获取数据。但根据我的console.logs,商店说我需要立即可用。它需要大约4个console.log才能看到数据。

现在我在设置一些新的本地状态之前使用了一些检查。我试图在DidMountComponent()函数中设置我的本地状态。它不起作用,bcz所需的数据不能直接使用。所以我在Render()

中设置了set local states函数
if(this.props.beers.beer.length>0 && this.state.beerArrayLoaded===false){
        this.setState({beerArrayLoaded:true});
        this.setState({BeerArray:this.props.beers.beer});

    }

我想知道在商店的数据可用之后是否有更合适的方法来设置新状态?

0 个答案:

没有答案