0

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')
2
  • 1
    You haven't defined freelancerLevel in the initial state. Commented Jun 13, 2019 at 10:34
  • where to define @zvona Commented Jun 13, 2019 at 10:35

1 Answer 1

4

You need to add freelancerLevel to state initialization:

this.state = { 
   isLoading: true,
   PickerValueHolder : '',
   freelancerLevel: []
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.