Skip to content Skip to sidebar Skip to footer

Updating State When Dropdown Select Is Changed React

This onChange event handles the selection of a dataschema then makes a subsequent request to get the queryschemas of the selected dataschema. handleChange is working correctly and

Solution 1:

You forgot to save selected value in the state (for select) and use event data (id) directly (in query url), not from state (setState is async, it will be updated later):

querySchemaChange = e => {
  const querySchema = this.state.querySchemas.find(querySchema => querySchema.name === e.target.value);
  if (querySchema) {
    const {id, name} = querySchema
    this.setState({
      querySchemaId : id, 
      querySchemaName: name
    });
    axios({
      method: 'get',
      url: `/dataschemas/${this.state.selectedId}/queryschemas/${id}/queries`,  

querySchemaName is used for current select value. Is saving querySchemaId needed now (not used in query)? Is it used elsewhere?

Post a Comment for "Updating State When Dropdown Select Is Changed React"