I am trying to access a state variable using a string within a React functional component.
For example, I know in Class components I can do the following to access state based on input:
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {
inputOne: null
}}
handleBlur(e, name) {
if (!this.state[name]) {
this.setState({
[`${name}Missing`]: true,
});
} else {
this.setState({
[`${name}Missing`]: false,
});
}
render() {
<input onBlur={(e) => this.handleBlur(e, "inputOne")}></input>
}
}
By using this.state[name] within the handleBlur function, I can access the inputOne state value dynamically.
How would I do something similar within a functional component?
Thanks for your time!