1

Response 'ItemData' will have n number of fields, i want to retrieve specific field, eg'itemNo' and set it to 'ItemData'. Instead of the whole response. So that i can use that in getOptionLabel of autocomplete. As I would like to set 'option => option' instead of 'option => option.specificFieldName' as per my requirement.

onChangeInputItemNo = value => {
         
             getItem(value).then(
                (response) => {
                    if (response.ok) {
                                        response.json()
                                            .then((responseData) => {
                                                console.log(responseData);
                                                console.log("hi");


                                                this.setState({
                                                    isLoading: false,
                                                    ItemData: responseData

                                                })
                                            });
                    }

                })
     }

I will be passing initial value to autocomplete box from child component, also i want the autocomplete to work as normal, if value needed to be changed. So i like to keep getOptionLabel as 'option' instead of 'option.specficFieldName'.

    <Autocomplete
        value={this.state.fromChild}                              
        options={this.state.ItemData}                                   
        getOptionLabel={option => option}   
        onChange={this.onChangeItemNo}                               
        renderInput={params => (
       <TextField {...params} onChange={e => this.onChangeInputItemNo(e.target.value)} fullWidth />
       )}
   />
                                
Sarahrb
  • 407
  • 11
  • 24

1 Answers1

0

If all you care about is the ItemNo in your responseData...

onChangeInputItemNo = value => {
      getItem(value).then((response) => {
         if (response.ok) {
           response.json().then(
             (responseData) => {
               this.setState({
                 isLoading: false,
                 ItemData: responseData?.ItemNo})
              });
            }
   })
         }
TR3
  • 327
  • 3
  • 6
  • Thank you. I incorporated (ItemData: responseData?.ItemNo) as per your suggestion. But now when I print 'ItemData', it returns undefined instead of returning itemNo. Earlier it was returning whole response with n number of fields. May i know what could be wrong. – Sarahrb Sep 08 '21 at 08:07
  • Then the `ItemNo` on that object doesn't exist. Could you show a log of your responseData? – TR3 Sep 08 '21 at 14:17
  • 1
    Hi, I have changed my backend to receive only the field that I am interested in and that resolved my problem. Thanks a lot for trying to help. – Sarahrb Sep 08 '21 at 20:23