you can refactor your input to each have a local state to handle its input value. at componentDidUpdate
you check if this.props.val
is a new value:
Input.js
import React, { Component } from "react";
export default class Input1 extends Component {
constructor(props) {
super(props);
this.inputRef = React.createRef();
this.state = {
inputValue: ''
};
}
handleKeyPress = e => {
if (e.keyCode === 13) {
this.inputRef.current.blur();
}
};
handleBlur = e => {
this.props.setVal(this.state.inputValue);
};
componentDidUpdate(prevProp) {
if (prevProp.val === this.props.val) return
// it will update input value for other input change
this.setState({ inputValue: this.props.val })
}
render() {
return (
<div>
<input
spellCheck="false"
// use value instead of DefaultValue
value={this.state.inputValue}
ref={this.inputRef}
onKeyDown={e => this.handleKeyPress(e)}
onBlur={e => this.handleBlur(e)}
// add a onChange handler to update state
onChange={ e => this.setState({inputValue : e.target.value}) }
/>
</div>
);
}
}