Javascript 处于未定义React.js状态的变量

Javascript 处于未定义React.js状态的变量,javascript,reactjs,Javascript,Reactjs,我试图将API中的产品存储在React组件的状态中,然后将它们映射到组件的渲染函数中,但不知何故,这个.state.products是未定义的 这是我的组件代码(CategoryProducts.js): API类中的此方法: async get(_endpoint) { let response = await fetch(this.API_url + _endpoint); return response.json(); } 我希望有人能帮我解决这个

我试图将API中的产品存储在React组件的状态中,然后将它们映射到组件的渲染函数中,但不知何故,
这个.state.products
未定义的

这是我的组件代码(CategoryProducts.js):

API类中的此方法:

async get(_endpoint) {
        let response = await fetch(this.API_url + _endpoint);
        return response.json();
    }
我希望有人能帮我解决这个问题

第一次调用render()时,API调用可能还没有准备好。在render()函数中需要考虑这一点

{ !this.state.products && <SomeLoadingComponent /> }
最简单的方法是:

{this.state.products && this.state.products.map(function(item, i){
                        console.log(i); 
                    })}
只有当
This.state.products
具有某些值时,才会进行映射

通过在render()函数中添加类似的内容,还可以在加载产品时显示一些加载组件

{ !this.state.products && <SomeLoadingComponent /> }
{!this.state.products&&}
第一次调用render()时,API调用可能尚未准备就绪。在render()函数中需要考虑这一点

{ !this.state.products && <SomeLoadingComponent /> }
最简单的方法是:

{this.state.products && this.state.products.map(function(item, i){
                        console.log(i); 
                    })}
只有当
This.state.products
具有某些值时,才会进行映射

通过在render()函数中添加类似的内容,还可以在加载产品时显示一些加载组件

{ !this.state.products && <SomeLoadingComponent /> }
{!this.state.products&&}

您正在将组件willmount转换为一个承诺,以便RN不会执行该部分:

 async componentWillMount(){
        await this.products.getProductsInCategory(this.props.name).then(
            (val) => {
                this.setState({products: val}); 
            }
        )
    }
试试这个:

constructor(props){
        super(props); 
        this.state = {
            products: [] //Or whatever suits you best
         }
        this.products = new Products(); 
    }

 componentWillMount(){
        this.products.getProductsInCategory(this.props.name).then(
            (val) => {
                this.setState({products: val}); 
            }
        )
    }

您正在将组件Willmount转换为承诺,以便RN不会执行该部分:

 async componentWillMount(){
        await this.products.getProductsInCategory(this.props.name).then(
            (val) => {
                this.setState({products: val}); 
            }
        )
    }
试试这个:

constructor(props){
        super(props); 
        this.state = {
            products: [] //Or whatever suits you best
         }
        this.products = new Products(); 
    }

 componentWillMount(){
        this.products.getProductsInCategory(this.props.name).then(
            (val) => {
                this.setState({products: val}); 
            }
        )
    }
componentWillMount()在装入之前立即调用。它在render()之前调用,因此在此方法中同步调用setState()不会触发额外的渲染

您应该避免在生命周期方法中放置API调用

而是使用

componentWillMount()在装入之前立即调用。它在render()之前调用,因此在此方法中同步调用setState()不会触发额外的渲染

您应该避免在生命周期方法中放置API调用

而是使用


或者初始化他的状态:
this.state={products:[]}
。这也可以,是的。然而,作为后续工作,区分(a)尚未从API请求数据,或(b)已请求数据,但数据为空(即没有结果)通常是有用的。对情况(a)使用像
undefined
这样的东西,对情况(b)使用空数组或空对象。或者初始化他的状态:
this.state={products:[]}
。这同样有效,是的。然而,作为后续工作,区分(a)尚未从API请求数据,或(b)已请求数据,但数据为空(即没有结果)通常是有用的。对于情况(a),通常使用
未定义的
,而对于情况(b),则使用空数组或空对象。