react-filter-box icon indicating copy to clipboard operation
react-filter-box copied to clipboard

How to change options prop dynamically after load

Open panigrah opened this issue 5 years ago • 1 comments

I have react-filter-box embedded into a component that reads the options dynamically from a database on componentDidMount.

<ReactFilterBox options={this.state.optionsFromDatabase} />

does reinitialise the options.

panigrah avatar Jan 11 '20 05:01 panigrah

@panigrah this should be possible with key prop, so when you have a new options to load you can change the key value.

Example

class MyComponents extends React.Component {
 state = {
   key: 1,
   options: [],
  }

async componentDidMount() {
   const newOptions =  await getDBData()
   this.setState({
       key: this.state.key + 1,
       options: newOptions
   })
 }

 render() {
  return (
     <ReactFilterBox options={this.state.optionsFromDatabase} key={this.state.key} />
  )
 }
}

lakhansamani avatar Mar 31 '20 11:03 lakhansamani