$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Insert dynamically a React component in a div - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Insert dynamically a React component in a div - Stack Overflow

matteradmin13PV0评论

I'd like to insert dynamically several React ponent in a div HTML element. Every time that I click on button, I would like to add a new PeriodButtonContainer inside div with id="container".

This is what I've tried so far:

class Period extends React.Component {
  addPeriodHandler = () => {
    /****?????******/
  };

  render() {
    return (
      <div>
        <div id="container">
          <PeriodButtonContainer />
        </div>
        <div>
          <button id="addPeriod" onClick={this.addPeriodHandler}>
            Add a period
          </button>
        </div>
      </div>
    );
  }
}

Thanks in advance

I'd like to insert dynamically several React ponent in a div HTML element. Every time that I click on button, I would like to add a new PeriodButtonContainer inside div with id="container".

This is what I've tried so far:

class Period extends React.Component {
  addPeriodHandler = () => {
    /****?????******/
  };

  render() {
    return (
      <div>
        <div id="container">
          <PeriodButtonContainer />
        </div>
        <div>
          <button id="addPeriod" onClick={this.addPeriodHandler}>
            Add a period
          </button>
        </div>
      </div>
    );
  }
}

Thanks in advance

Share Improve this question edited Jul 11, 2018 at 21:23 Tholle 113k22 gold badges208 silver badges197 bronze badges asked Jul 11, 2018 at 21:19 M.MorrisM.Morris 1473 silver badges13 bronze badges 1
  • you should increment a counter in your state, create an array of the length of the counter and map it to your element – Axnyff Commented Jul 11, 2018 at 21:22
Add a ment  | 

1 Answer 1

Reset to default 6

You could keep a counter that you increment on each button click, and create that many PeriodButtonContainer ponents in your render method with e.g. Array.from.

Example

class Period extends React.Component {
  state = { periods: 1 };

  addPeriodHandler = () => {
    this.setState(previousState => {
      return { periods: previousState.periods + 1 };
    });
  };

  render() {
    return (
      <div>
        <div id="container">
          {Array.from({ length: this.state.periods }, (_, index) => (
            <PeriodButtonContainer key={index} />
          ))}
        </div>
        <div>
          <button id="addPeriod" onClick={this.addPeriodHandler}>
            Add a period
          </button>
        </div>
      </div>
    );
  }
}
Post a comment

comment list (0)

  1. No comments so far