M
M
mortyy2017-08-16 19:17:32
React
mortyy, 2017-08-16 19:17:32

Why is the selected value lost in SelectField (material-ui) when updating data?

There is such a component

class Dropdown extends Component {
  constructor(props) {
    super(props)
    this.state = {value: this.props.value}
    this.renderMenuItems = this.renderMenuItems.bind(this)
  }

  renderMenuItems(accounts) {
    return accounts.map((account) => (
      <MenuItem
        key={account.id}
        insetChildren={true}
        primaryText={<this.props.template {...account} />}
        value={account}
      />
    ));
  }

  render() {
    const {value} = this.props;
    return (
      <div className="transaction-dropdown">
        <Translate className="dropdown-title" value={_.get(this.props, 'title', null)}/>
        <div className="dropdown-content">
          <SelectField
            errorText={this.props.error}
            value={value}
            fullWidth={true}
            onChange={(e, index, item) => {
              this.setState({value: item})
              this.props.select(item)
            }}>
            {this.renderMenuItems(this.props.data || [])}
          </SelectField>
        </div>
      </div>
    )
  }
}

export default Dropdown

When this.props.data is updated, the selected value is reset, although it is present in the new array. You can also see that props selectField hits it.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question