简体   繁体   English

“反应”按钮最多渲染一次

[英]React button does not render more than once

I have a button for rendering new components (actually charts, i have simplified the example to only show text) which adds a text to the page the first time it's clicked, but won't add any new text-objects to the page with further clicks. 我有一个用于渲染新组件的按钮(实际上是图表,我已经简化了示例,只显示了文本),它在第一次单击时在页面上添加了文本,但不会在页面上添加任何新的文本对象。点击。

I've tested to see if the function is actually running when I'm pressing it multiple times by making it add elements to an array which it does, so why isn't it rendering new text-objects to the page when clicked multiple times? 我已经测试过该函数是否多次运行,方法是使该函数向数组中添加元素,以使其多次运行,因此为什么多次单击时不将新的文本对象呈现给页面? ?

I might be missing something fundamental and would be grateful for any explanation. 我可能缺少一些基本知识,不胜感激。

import React from 'react';
import './App.css';

class NewChart extends React.Component {
  render() {
    return (
      <div>Text</div>
    );
  }
}

class Button extends React.Component {
  render() {
    return (
      <button {...this.props}>
        Add chart
      </button>
    );
  }
}

class ChartApp extends React.Component {
  constructor() {
    super();
    this.state = {
        clicked: false
    };
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState({
        clicked: true
    });
  }

  render() {
    return (
      <div>
        <Button onClick={this.handleClick} />
        {this.state.clicked ? <NewChart />: null}
      </div>
    );
  }
};

export default React.createClass({
  render: function () {
    return (
      <div>
        <ChartApp>
        </ChartApp>
      </div>
    )
  }
});

You are presently using a button which sets a single flag to true, and then rendering a component when that flag is true. 当前,您正在使用将单个标志设置为true的按钮,然后在该标志为true时呈现组件。 If you would like to render multiple components, you need some aspect of your state to relate to how many components you want to render, and then render based on that. 如果要渲染多个组件,则需要状态的某些方面与要渲染的组件数量有关,然后基于该渲染。 For example as you mention, if you either use an array or perhaps a counter variable, you could then use either .map or even a for loop to render multiple components. 例如,正如您提到的那样,如果您使用数组或计数器变量,则可以使用.map甚至是for循环来呈现多个组件。 Right now you're only asking React to conditionally render a single NewChart component. 现在,您只要求React有条件地呈现单个NewChart组件。

The problem is that you are not adding new items, but just rendering or not depending on the value of checked . 问题是,你是不是增加新的项目,而只是呈现或不依赖于价值的checked The trick is to have an array of elements to render and add one more element each time you click on the button. 诀窍是每次单击按钮时都有一组要渲染的元素并添加一个以上的元素。

Here you have an working example: 这里有一个工作示例:

import React from 'react';
import './App.css';

class NewChart extends React.Component {
  render() {
    return (
      <div key={this.props.key}>Text</div>
    );
  }
}

class Button extends React.Component {
  render() {
    return (
      <button {...this.props}>
        Add chart
          </button>
    );
  }
}

class ChartApp extends React.Component {
  constructor() {
    super();
    this.state = {
      elements: []
    };
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    var newArray = this.state.elements.slice();
    newArray.push(<NewChart key={this.state.elements.length + 1}/>);
    this.setState({
      elements: newArray
    });
  }

  render() {
    return (
      <div>
        <Button onClick={this.handleClick} />
        {this.state.elements.map((elem) => {
          return elem;
        })}
      </div>
    );
  }
}

class App extends React.Component {
  render () {
    return (
      <div>
        <ChartApp />
      </div>
    )
  }
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM