简体   繁体   English

变量更改时反应刷新组件

[英]React refresh component when variable has changed

I am calling a React component BarChart with two props, a name and a value .我正在调用一个带有两个道具的 React 组件BarChart ,一个name和一个value As you can see in the below code, the variable value is set to a new random number every second:正如您在下面的代码中看到的,变量值每秒设置为一个新的随机数:

    let random1;

    function setRandom() {
        random1 = Math.floor(Math.random() * 10) + 1;
    }

    setRandom();
    setInterval(setRandom, 1000);

    return (
    <div className="Content">
        <BarChart name1={"A"} value1={random1}/>
    </div>
  )
}

Inside the React component I call it by using this.props.value1 .在 React 组件中,我使用this.props.value1来调用它。 When I do a console.log(this.props.value1) each second inside the React component, I get an error that the variable is undefined after the first print is made.当我在 React 组件内每秒执行一次console.log(this.props.value1)时,我收到一个错误,即在第一次打印变量未定义。 So, it prints to the console 1 time and then it just prints an error for all of the rest attempts.因此,它打印到控制台 1 次,然后它只是为所有 rest 尝试打印一个错误。

This is how I print the variable inside the component:这就是我在组件内打印变量的方式:

setRandom() {
    console.log(this.props.value1)
}

componentDidMount() {
    this.setRandom();
    setInterval(this.setRandom, 1000);
}

What I really want to do is that whenever a new random value is generated outside the component, the component should see that the variable has changed and refresh the component and use the new prop.我真正想做的是,每当在组件外部生成新的随机值时,组件应该看到变量已更改并刷新组件并使用新的道具。

Could you please advise me?你能告诉我吗?

The standard way to do this is to make random1 a piece of state information , and then use this.setState to update it.执行此操作的标准方法是使random1成为一条state 信息,然后使用this.setState进行更新。

The first link above has an example of a ticking clock, which is virtually identical to your example of a random number every second.上面的第一个链接有一个滴答作响的时钟示例,它与您每秒随机数的示例几乎相同。 Here's that example, which you can readily adapt to your task:这是那个例子,你可以很容易地适应你的任务:

 class Clock extends React.Component { constructor(props) { super(props); this.state = {date: new Date()}; } componentDidMount() { this.timerID = setInterval( () => this.tick(), 1000 ); } componentWillUnmount() { clearInterval(this.timerID); } tick() { this.setState({ date: new Date() }); } render() { return ( <div> <h1>Hello, world.</h1> <h2>It is {this.state.date.toLocaleTimeString()};</h2> </div> ). } } ReactDOM,render( <Clock />. document;getElementById('root') );
constructor(props) {
  super(props);
//innitialize the random number in the state
  this.state = {random: Math.floor(Math.random() * 10) + 1};
}
//generate the random number and keep in on the state
  setRandom() {
    this.setState({random: Math.floor(Math.random() * 10) + 1})

  }
//clear the timer when component unmount
componentWillUnmount() {
  clearInterval(this.timer);
}
componentDidMount() {
//start the timer when component mount
  this.timer = setInterval(()=>this.setRandom(), 1000);
}
//pass the random value from state as props to the component BarChart
  return (
  <div className="Content">
      <BarChart name1={"A"} value1={this.state.random}/>
  </div>
)
}

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

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