小编Bar*_*101的帖子

跨组件 React 发送多个道具

我试图将两个变量从组件“游戏”发送到组件“应用程序”,但我不确定如何一次发送多个道具。

这是我所拥有的:

//App Component

class App extends Component {

  constructor(props) {
    super(props)
    this.state = {
      score: 0,
    }

    this.changeScore = this.changeScore.bind(this)
  }

  changeScore(newScore) {
    this.setState(prevState => ({
      score: prevState.score + newScore
    }))
  }

  render() {
    return(
      <div>
        <Game onClick={this.changeScore}/>
        <Score score={this.state.score}/>
      </div>
    )
  }
}
Run Code Online (Sandbox Code Playgroud)
//Game Componenet 

class Game extends Component {

    constructor(props) {
        super(props)
        this.state = {
            score: 0,
        }
        this.handleClick = this.handleClick.bind(this)
    }

    handleClick() {
        console.log('Clicked')
        this.props.onClick(this.state.score)

    }

    render() {
        return(
            <div>
                <button onClick={this.handleClick}> Score Button </button>
            </div> …
Run Code Online (Sandbox Code Playgroud)

javascript components reactjs react-props react-state

3
推荐指数
1
解决办法
7692
查看次数