How do I reset the defaultValue for a React input

前端 未结 2 1287
野的像风
野的像风 2021-01-19 19:54

I have a set of React input elements that have a defaultValue set. The values are updated with an onBlur event.

I also have another action on the page that updates

2条回答
  •  面向向阳花
    2021-01-19 20:55

    I've solved this by using both onBlur and onChange and only keeping track of the currently active input element in the state.

    If there is a way to reset the module so that it re-displays the new default values then I'll mark that as correct.

    state = {
      inFocusIndex: null,
      inFocusDisplayOrder: 0,
    };
    
    
    onOrderBlur() {
      const productRow = this.props.products[this.state.inFocusIndex];
      const oldDisplayORder = productRow.displayOrder;
      // This can change all the display order values in the products array
      this.props.updateDisplayOrder(
        this.props.groupId,
        productRow.productGroupLinkId,
        oldDisplayORder,
        this.state.inFocusDisplayOrder
      );
      this.setState({ inFocusIndex: null });
    }
    
    onOrderChanged(index, event) {
      this.setState({
        inFocusIndex: index,
        inFocusDisplayOrder: event.target.value,
      });
    }
    

    In the render function:

    {this.props.products.map((row, index) => {
      return (
        
      );
    })}
    

提交回复
热议问题