React + Redux: Why are props populated on third time load?

六月ゝ 毕业季﹏ 提交于 2019-12-11 09:46:28

问题


See my console log PROPS and the first two instances PROPS are empty then the third it is populated?

Why is that and is it possible to get around this so that the PROPS are populated on initial load?

My current stack for the application is React, Redux and using Axios to fetch JSON data from a CMS.

Screenshot:

EDIT!!!!!

Here is my edit of my component with render function - see Footer for getData method:

componentWillMount() {
        //Fetch Ad Products Data
        this.props.dispatch(fetchAdProductsData())

        //Fetch List Ad Products Data
        this.props.dispatch(fetchListAdProductData())

        //Fetch Fox Footer Data
        this.props.dispatch(fetchFoxFooterData());

    }


    getData(prop) {
        if (prop.data === undefined) {
            return [{}];
        }

        return prop.data;
    }


    render(){
        let foxFooterData = this.props.foxFooterData;
        let listAdProductsData = this.props.listAdProductsData;

        return (
            <div className="ad-products-wrap container no-padding col-xs-12">
                <Header />
                <HeroModule />
                <HeroDetail />
                <ProductCategoryLeft />
                <ProductCategoryNavigation />
                <ProductCategoryRight />
                <ShowcaseModule />
                <NewsModule />
                <ContactModule />
                <Footer data={this.getData(foxFooterData)} />
            </div>
        )
    }

回答1:


1.) Call FETCH_FOXFOOTER_DATA_COMPLETE componentDidMounthttps://reactjs.org/docs/react-component.html#componentdidmount.

2.) Remove this.getData() and leverage React's declarative nature. Inside the render function in your question pass props to <Footer/> and let that component handle missing props:

In <Footer/> :

render() {
  return (
    <footer>
      {props.data.length ? <DisplayDetailsThatNeedProps {...props} /> : <Loading />}
    </footer>
  )
}


来源:https://stackoverflow.com/questions/47040303/react-redux-why-are-props-populated-on-third-time-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!