So I was playing around in React and tried to make an app where you can display and modify any property in the component state, from the component itself. Basically, you enter a key (eg "foo"), and then the app will load the current value at that key (eg this.state["foo"]) into a textarea, where you can modify the value.
Here is the sandbox: https://codesandbox.io/s/twilight-bird-6z6cx And here is the code used in the sandbox:
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
class App extends React.Component {
state = {
inputVal: ""
};
onKeyInputChange(e) {
this.setState({ key: e.target.value });
}
onValInputChange(e) {
this.setState({ [this.state.key]: e.target.value });
}
render() {
return (
<div className="App">
<div>
key: <input onChange={this.onKeyInputChange.bind(this)} />
</div>
<div>
value:
<textarea
onChange={this.onValInputChange.bind(this)}
value={this.state[this.state.key]}
/>
</div>
<h2>key: {this.state.key}</h2>
<h2>value: {this.state[this.state.key]}</h2>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Notice how the <textarea>'s value and the <h2>'s value are both set to this.state[this.state.key], so they should always show the same value. However, if you change the text value at say, key "foo", and then change to key "fooo", the h2's value will change but not the textarea. To put it more clearly:
- type "foo" into the
keyinput field - type "hello world" into the textarea
- type "fooo" into the
keyinput field - notice how the
<h2>'s value is now blank, but the textarea still shows "hello world"