S.Hashmi
S.Hashmi

Reputation: 501

Getting an error about undefined is not object in React native

I am getting list of data from Rest api an i am setting that data in the picker (drop down spinner).but its not working showing error please check my code and suggests some solution.

 constructor(props)
    {
      super(props);
      this.state = { 
      isLoading: true,
      PickerValueHolder : ''
     }
    }
 componentDidMount() {
        this.FreelancerLevelSpinner();
 }

            FreelancerLevelSpinner = async ()=>{
                return fetch('taxonomies/get_list?list=freelancer_level')
                .then((response) => response.json())
                .then((responseJson) => {
                this.setState({
                    freelancerLevel: responseJson
                }, function() {
                    // In this block you can do something with new state.
                });
                })
                .catch((error) => {
                console.error(error);
                });
            }

here i am setting the data in the picker...

<Picker
            selectedValue={this.state.PickerValueHolder}

            onValueChange={(itemValue, itemIndex) => this.setState({PickerValueHolder: itemValue})} >

            { this.state.freelancerLevel.map((item, key)=>(
            <Picker.Item label={item.title} value={item.value} key={key} />)
            )}

          </Picker>

It is showing this error...

TypeError: TypeError: undefined is not an object (evaluating 'this.state.freelancerLevel.map')

Upvotes: 0

Views: 123

Answers (1)

Mykybo
Mykybo

Reputation: 1499

You need to add freelancerLevel to state initialization:

this.state = { 
   isLoading: true,
   PickerValueHolder : '',
   freelancerLevel: []
}

Upvotes: 4

Related Questions