Skip to content Skip to sidebar Skip to footer

React Native - Typeerror: Null Is Not An Object

I'm still learning react native and I'm trying to retreive datas from API and return it as custom radio button but if I do call the API, I got this error: null is not an object (

Solution 1:

Initial state of activities is null. So until you get a response from your api React is rendering that part of your code with the null value. You can either give activities: [] as initial value or you can before your map function if its null or not such as

{this.props.activities && this.props.activities.map((val, index) => {...

If you are going to use activities: [] then you can still make a check before your map, altough it is optional but still good, such as;

{this.props.activities.length && this.props.activities.map((val, index) => {...

Solution 2:

Where is this.props.activities coming from?

Is it asynchronous, as it appears that it is null at a certain point.

If you add the following line you should no longer see this error.

this.props.activities && this.props.activities.map(.....

Solution 3:

Paul answer is a bulletproof but you can use default value too to avoid falsy values

YourComponent.defaultProps = {
    activities: []
}

Post a Comment for "React Native - Typeerror: Null Is Not An Object"