最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Material UI Autocomplete component not showing values from react state - Stack Overflow

programmeradmin8浏览0评论

I am trying to get value from the state for materialUI's autoplete ponent.

I am facing the following problem : -

Autoplte working fine for selecting the value and with onChange function it saving it into the state too. But when I refresh my page/ re-render it is not showing value on the textfeild(from saved state):

<Autoplete
    name={"TideLocation"}
    disabled={p.disabled}
    options={data_source}
    getOptionLabel={option => option.text}
    inputValue={this.state.tidelocation_searchtext}
    onChange={_this.handleUpdateTideLocationField}
    onNewRequest={_this.handleChangeTideLocation}
    onBlur={_this.handleBlurTideLocationField}
    onUpdateInput={_this.handleUpdateTideLocationField}
      renderInput={(params) => (
       <TextField className="autoCompleteTxt"{...params} label="Location" />
    )}
/>

I tried with the debugger and found its getting value in this.state.tidelocation_searchtext but failed to set it with params.

Thanks in advance !! Ps: I tried with defaultValue and search text nothing worked for me

following is my ONchangeFunction

  handleUpdateTideLocationField = (str, value) => {
        debugger
        this.setState({tidelocation_searchtext: value.text});
    }

after selecting a value,following value saved in sate :

tidelocation_searchtext: "Auckland"

I am trying to get value from the state for materialUI's autoplete ponent.

I am facing the following problem : -

Autoplte working fine for selecting the value and with onChange function it saving it into the state too. But when I refresh my page/ re-render it is not showing value on the textfeild(from saved state):

<Autoplete
    name={"TideLocation"}
    disabled={p.disabled}
    options={data_source}
    getOptionLabel={option => option.text}
    inputValue={this.state.tidelocation_searchtext}
    onChange={_this.handleUpdateTideLocationField}
    onNewRequest={_this.handleChangeTideLocation}
    onBlur={_this.handleBlurTideLocationField}
    onUpdateInput={_this.handleUpdateTideLocationField}
      renderInput={(params) => (
       <TextField className="autoCompleteTxt"{...params} label="Location" />
    )}
/>

I tried with the debugger and found its getting value in this.state.tidelocation_searchtext but failed to set it with params.

Thanks in advance !! Ps: I tried with defaultValue and search text nothing worked for me

following is my ONchangeFunction

  handleUpdateTideLocationField = (str, value) => {
        debugger
        this.setState({tidelocation_searchtext: value.text});
    }

after selecting a value,following value saved in sate :

tidelocation_searchtext: "Auckland"
Share Improve this question edited Jan 28, 2020 at 11:22 DevCo asked Jan 28, 2020 at 10:58 DevCoDevCo 4791 gold badge3 silver badges19 bronze badges 2
  • How to create a Minimal, Reproducible Example, we can't guess what are the state values and how you rendering the ponent – Dennis Vash Commented Jan 28, 2020 at 11:03
  • Ok I will add some more information to it – DevCo Commented Jan 28, 2020 at 11:05
Add a ment  | 

5 Answers 5

Reset to default 8

So I found the solution all by myself by doing research and several hit and try, following is the solution of my problem:

<Autoplete
  name={"TideLocation"}
  disabled={p.disabled}
  options={data_source.map(option=>option.text)} 
  defaultValue={this.state.tidelocation_searchtext}
  onChange={_this.handleUpdateTideLocationField}
  onNewRequest={_this.handleChangeTideLocation}
  onBlur={_this.handleBlurTideLocationField}
  onUpdateInput={_this.handleUpdateTideLocationField}
  renderInput={(params) => (
  <TextField className="autoCompleteTxt"{...params} label="Location" />
 )}
/>

Basically I was doing the following things wrong :

1.There is no need of using input inputValue={this.state.tidelocation_searchtext}& getOptionLabel={option => option.text}

2.as my data is in object form I have to convert it into a string so default value can match this from the state value options={data_source.map(option=>option.text)}

Thank you all for your valuable support and solution !!

Removing inputValue has worked for me, even when passing object as options.

Using: "@material-ui/core": "^4.12.3"

You are right, if object type options are passed, material-ui's AutoComplete ponent does not seem to reflect the value when mounted. (Perhaps a bug?)

I was able to get around this by passing the proper characters to inputValue.

@RebelCoder

Maybe you should have initialized tidelocation_searchtext.

For me it was ing from the dropdown z-index which was hidden by another css behaviour.

I added this in a css file :

/* Dropdown MUI Component Autoplete*/
div[role="presentation"].MuiAutoplete-popper {
  z-index: 1000000;
}

And it appeared finally. A bit hacky, but I think it was caused by another library that had something of that kind.

Note that I added several css elements to the selector, because just using the class wasn't enough.

If data/state not saved externally f.e. in local storage then it will be lost on page refresh, always. It's normal - like RAM memory without power - it (page/app state) only exists in memory !!

It's like using using cookie to keep you logged in.

If you really need such functionality then use f.e. redux-persist

发布评论

评论列表(0)

  1. 暂无评论