React: trigger onChange if input value is changing by state?

后端 未结 9 1915
我在风中等你
我在风中等你 2020-11-30 04:34

Edit: I don\'t want to call handleChange only if the button has been clicked. It has nothing to do with handleClick. I gave an example in the @shubhakhatri

9条回答
  •  隐瞒了意图╮
    2020-11-30 05:06

    Solution Working in the Year 2020:

    I tried the other solutions and nothing worked. This is because of input logic in React.js has been changed. For detail, you can see this link: https://hustle.bizongo.in/simulate-react-on-change-on-controlled-components-baa336920e04.

    In short, when we change the value of input by changing state and then dispatch a change event then React will register both the setState and the event and consider it a duplicate event and swallow it.

    The solution is to call native value setter on input (See setNativeValue function in following code)

    Example Code

    import React, { Component } from 'react'
    export class CustomInput extends Component {
    
        inputElement = null;
    
        // THIS FUNCTION CALLS NATIVE VALUE SETTER
        setNativeValue(element, value) {
            const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set;
            const prototype = Object.getPrototypeOf(element);
            const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
    
            if (valueSetter && valueSetter !== prototypeValueSetter) {
                prototypeValueSetter.call(element, value);
            } else {
                valueSetter.call(element, value);
            }
        }
    
    
        constructor(props) {
            super(props);
    
            this.state = {
                inputValue: this.props.value,
            };
        }
    
        addToInput = (valueToAdd) => {
            this.setNativeValue(this.inputElement, +this.state.inputValue + +valueToAdd);
            this.inputElement.dispatchEvent(new Event('input', { bubbles: true }));
        };
    
        handleChange = e => {
            console.log(e);
            this.setState({ inputValue: e.target.value });
            this.props.onChange(e);
        };
    
        render() {
            return (
                
    { this.inputElement = input }} name={this.props.name} value={this.state.inputValue} onChange={this.handleChange}>
    ) } } export default CustomInput

    Result

提交回复
热议问题