E
E
EroticDeer2019-09-02 01:24:00
React
EroticDeer, 2019-09-02 01:24:00

Is it possible to somehow pass the name of the state element from the child to the parent in Raect?

Is it possible to somehow pass the name of the state element from the child to the parent in Raect?
Parent:

class Container extends Component {
  constructor() {
    super();
    this.state = {
      test: 1
    };
    this.buyEgg = this.buyEgg.bind(this);
  }


buyEgg(value) {
    this.setState({ value: 2 });
  }

  
  render() {
    return (
      <div>
        <TestShop buyEgg={this.buyEgg} test={this.state.test} />
      </div>
    );
  }

Child:
class TestShop extends Component {
  constructor(props) {
    super();
    this.state = {
      
    };
  }

  render() {
    return (
      <div>
        <h1>{this.props.test}</h1>
        <button value="test" onClick={() => this.props.buyEgg('test')}>
          Click
        </button>
      </div>
    );
  }
}

those. we need a universal function, which is passed the name of the state element and changes to some value

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bpGusar, 2019-09-10
@EroticDeer

buyEgg(value) {
    this.setState({ [value]: 2 });
  }

if I understood correctly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question