0
How do I take a function parameter and use it to pick up a variable? For example, in this code:
import React, { Component } from 'react';
export default class Home extends Component {
state = {
name: 'joao',
age: 18
}
set = (s, v) => {
this.setState({s: v})
}
get = (s) => {
return this.state.s;
}
render() {
return(
<div>
<button onClick={() => this.set('name', 'maria')}>Mudar Nome</button>
<button onClick={() => this.set('age', 20)}>Mudar Idade</button>
{this.get('name')}
{this.get('age')}
</div>
);
};
}
I wanted to pass the get('name') and the get do something like: Return this.state.name; But it does not return this, it seems that it uses only the s
How do you do that? What’s the name of it? (this code I made just for example)
Exactly what I needed, but it only worked on set, no get gave problem: . /src/pages/home/index.js Line 19: Parsing error: Unexpected token 17 | } 18 | get = (s) => { > 19 | Return this.state. [s]; | 20 | } 21 | // template 22 | render() {
– rogomazagu
How did you write the get?
– Caio Felipe Pereira
https://hastebin.com/oqerulexol.js
– rogomazagu