首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当父组件的状态更新时,子组件的属性不会更新

当父组件的状态更新时,子组件的属性不会更新
EN

Stack Overflow用户
提问于 2020-02-12 21:06:30
回答 1查看 403关注 0票数 3

我正在做一个Connect-Four游戏。每列(组件)从父组件接收当前播放器颜色作为道具。我在每个列中都有一个回调函数,每次单击列时都会更改当前的播放器状态,但由于某些原因,这些列不会接受新的父状态作为更新后的道具。

代码语言:javascript
复制
class App extends React.Component {
    constructor() {
        super()

        this.state = {
            currentPlayer: 'red',
            board: null,
        }
    }

    changePlayer = () => {
        this.state.currentPlayer === 'red' ?
            this.setState({
                currentPlayer: 'yellow'
            }) :
            this.setState({
                currentPlayer: 'red'
            })
    }

    componentDidMount() {
        let newBoard = [];
        for(let x = 0; x < 7; x++) {
            newBoard.push(<Column 
                key={`column ${x}`} 
                currentPlayer={this.state.currentPlayer} 
                changePlayer={this.changePlayer}
                x={x} 
            />)
        }

        this.setState({
            board: newBoard,
        })
    }

    render() {
        return(
            <div className="app">
                <div className="board">
                    {this.state.board}
                </div>
            </div>
        )
    }
}
代码语言:javascript
复制
class Column extends React.Component {
    constructor(props) {
        super(props)

        this.state = {
            colors: ['white', 'white', 'white', 'white', 'white', 'white']
        }
    }

    handleClick = () => {
        for(let i = 0; i < 6; i++) {
            if(this.state.colors[i] === 'white') {
                let newColors = this.state.colors;
                newColors[i] = this.props.currentPlayer;
                this.setState({
                    colors: newColors
                })
                break;
            }
        }

        this.props.changePlayer();
    }

    render() {
        let column = [];
        for(let y = 5; y >= 0; y--) {
            column.push(<Tile 
                key={`${this.props.x},${y}`} 
                x={this.props.x} 
                y={y} 
                color={this.state.colors[y]}
            />)
        }

        return(
            <div className="column" onClick={() => this.handleClick()}>
                {column}
            </div>
        )
    }
}

我认为问题出在列是用componentDidMount生命周期钩子创建的吗?如果是这样的话,我怎么能在不改变太多代码结构的情况下解决这个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-12 21:19:36

不清楚您的代码在哪里失败,但是:

代码语言:javascript
复制
    // Here you are setting a reference to the array in state, not a copy
    let newColors = this.state.colors;
    // Here you are mutating directly the state (antipattern!)
    newColors[i] = this.props.currentPlayer;
    // You are setting the reference to the array that has already mutated (prevState === nextState)
    this.setState({
     colors: newColors
    });

取而代之的是:

代码语言:javascript
复制
    // Make a COPY of your array instead of referencing it
    let newColors = [...this.state.colors];
    // Here you are mutating your CLONED array
    newColors[i] = this.props.currentPlayer;
    // You are setting the NEW color array in the state
    this.setState({
     colors: newColors
    });

好了,我知道你的问题了。

App.js中的更改:

代码语言:javascript
复制
for(let x = 0; x < 7; x++) {
    newBoard.push(<Column 
        key={`column ${x}`}
        // retrieve value with a method as below 
        currentPlayer={() => this.state.currentPlayer} 
        changePlayer={this.changePlayer}
        x={x} 
    />)
}

在Columns.js中:

代码语言:javascript
复制
newColors[i] = this.props.currentPlayer();

工作示例:

https://stackblitz.com/edit/react-zzoqzj?file=Column.js

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60189074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档