简体   繁体   English

React.js:如何添加多个实例

[英]Reactjs: How to add multiple instances

Not sure the Title of this question describes what I'm struggling with. 不确定此问题的标题是否描述了我正在努力解决的问题。 This is a jockey program. 这是一个赛马程序。 If I want to add multiple jockeys competing to finish a race, How do I do that? 如果我想增加多个骑师参加比赛,该怎么办? It was working for just one jockey, however, when I try to add a second/multiple jockeys, I get issues. 它仅适用于一个骑师,但是,当我尝试添加第二个/多个骑师时,出现了问题。 Where am I failing in the logic? 我在逻辑上哪里失败了?

App.js App.js

 import React, { Component } from 'react'; import logo from '../../images/logo.svg'; import { Jockey } from './Jockey'; import './App.css'; export class App extends Component { // handleClick = () => { // // const that = this; // this.interval = setInterval(() => { // this.setState((previousState) => { // if(previousState.progress >= 99){ // return { progress:100 } // } // return { progress: previousState.progress + 1 } // }); // }, this.state.interval); // } render() { const Buttons = () => ( <div className="App-buttons"> <button className="ui primary button" onClick={this.handleClick}>Start</button> <button className="ui button">Reset</button> </div> ); return ( <div className="App"> <header className="App-header"> <img src={logo} className="App-logo" alt="logo" /> <h1 className="App-title">Welcome to the React Race Hustle</h1> </header> {/* <Buttons /> */} <Jockey /> {/* <Jockey /> */} </div> ); } } 

Jockey.js 骑师

 import React, { Component } from 'react'; import Progress from 'react-progressbar'; export class Jockey extends Component { constructor(props){ super(props); this.state = { avatar: "https://avatars1.githubusercontent.com/u/3757315?v=4", interval: Math.floor(Math.random() * 500), progress: 0, } this.handleClick = this.handleClick.bind(this); } handleClick = () => { // const that = this; this.interval = setInterval(() => { this.setState((previousState) => { if(previousState.progress >= 99){ return { progress:100 } } return { progress: previousState.progress + 1 } }); }, this.state.interval); } render() { // const Buttons = () => ( // <div className="App-buttons"> // <button className="ui primary button" onClick={this.handleClick}>Start</button> // <button className="ui button">Reset</button> // </div> // ); return ( <div> {/* <Buttons /> */} <div className="App-field"> <img src={this.state.avatar} alt=""/> <Progress className="App-progress" completed={this.state.progress}/> </div> </div> ); } } 

Actually in your code, nothing can trigger the handleClick event in Jockey class, I assume you want one button in App class to start all jockeys at the same time. 实际上,在您的代码中,没有什么可以触发Jockey类中的handleClick事件,我假设您希望App类中的一个按钮可以同时启动所有jockey。

There's different ways to do this : 有不同的方法可以做到这一点:

1st Method : Use a "toggle prop" to fire event in children : 第一种方法: 使用“切换道具”触发儿童事件:

in Parent : 在家长:

handleClick = () => {
    this.setState({ start: true });
}
...
<Jockey start={this.state.start} />

in Child : 在儿童:

componentWillReceiveProps(nextProps) {
    if (this.props.start === false && nextProps.start === true) {
        this.startInterval(); // handleClick function in your case
    }
}

2nd Method : Move the logic to the parent, be sure to use a different names to declare multiple setInterval. 第二种方法: 将逻辑移到父级,请确保使用不同的名称来声明多个setInterval。

3rd Method : Use the ref attribute to execute a child method : 第三种方法: 使用ref属性执行子方法:

in Parent : 在家长:

handleClick = () => {
    this.jockey1.handleClick();
    this.jockey2.handleClick();
}
...
<Jockey ref={jockey => { this.jockey1 = jockey } />
<Jockey ref={jockey => { this.jockey2 = jockey } />

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

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