0

Instead of using the original way like this.state, I want to use the combined String.

import React, { Component } from "react";

class Test extends Component {
  constructor(props) {
    super(props);
    this.state = {
      Monday100: "Monday100",
    };
  }

  render() {
    let day = "Monday";
    let number = "100";
    let dayAndNumber = day + number;

    return (
      <div>
        {/* I Want To Do It Like This */}
        {/* {this.state.dayAndNumber} */}
        {/* Original Way */}
        {this.state.Monday100}
      </div>
    );
  }
}

export default Test;
BTSM
  • 1,585
  • 8
  • 12
  • 26
  • 2
    Aren't you just asking https://stackoverflow.com/q/4244896/3001761? React state or not, that's a vanilla JavaScript object. – jonrsharpe May 27 '22 at 13:00

1 Answers1

0

You can accss by this.state[dayAndNumber]

import React, { Component } from "react";

class Test extends Component {
  constructor(props) {
    super(props);
    this.state = {
      Monday100: "Monday100",
    };
  }

  render() {
    let day = "Monday";
    let number = "100";
    let dayAndNumber = day + number;

    return (
      <div>
        {/* I Want To Do It Like This */}
        {/* {this.state.dayAndNumber} */}
        {/* Original Way */}
        {this.state[dayAndNumber]}
      </div>
    );
  }
}

export default Test;
BTSM
  • 1,585
  • 8
  • 12
  • 26