Hamed mayahian
Hamed mayahian

Reputation: 2323

can't retrieve woocommerce product data in react-native

I'm just trying to get sample product data from woocommerce API.

this is my code:

import React, { Component } from 'react';
import {
    AppRegistry,
    Text,
    View
} from 'react-native';
import Api from './WooCommerce/Api.js'

export default class FetchExample extends Component{
    constructor() {
        super();

    }
    render() {
        return (
            <View>
                <Text>{this.state.date.name}</Text>

            </View>
        );
    }
    componentWillMount() {
        this.setState.date = Api.get('products/122609')


    }
}



AppRegistry.registerComponent('fetchExample', () => FetchExample);

And here is a error:

Error Message

Upvotes: 0

Views: 221

Answers (1)

linasmnew
linasmnew

Reputation: 3977

Initialise your state with default values inside of your constructor:

this.state = { date: '...' };

Then change your state updating logic from direct assignment to this.setState, since direct assignment will lead to unexpected behaviour:

this.setState({ date: Api.get('products/122609') });

Upvotes: 1

Related Questions