简体   繁体   English

子组件未重新渲染

[英]Child Components are not being re-rendered

I'm new to React. 我是React的新手。 As a learning exercise I'm building an chess application 作为学习练习,我正在构建一个国际象棋应用程序

I want to change the DOM of child based on state in parent. 我想根据父级的状态更改子级的DOM。 Currently there is no change in child components on change of state in parent. 当前,父组件的状态更改后,子组件没有任何更改。

Parent Component 父组件

class Game extends Component{
    constructor(){
        super();
        this.state = {
            game :{ 
                board_position = { 'One' : ''}
            }
        }

        this.handleClick = this.handleClick.bind(this);
    }

    handleClick(){
        let p = this.state.game.board_position;

        // some computations on p
        p = { board_position : { 'One' : Math.random() } }

        this.setState({
            game : p
        })

        //this.forceUpdate(); // Just trying different methods to make it work
    }

    render(){
        return(
            <div onClick={(e) => this.handleClick(e) }>
                <Piece {...this.state.game.board_position} />
            </div>
        )
    }
}

Child Component 子组件

Class Piece extends Component{

    constructor(){
        super(props);
        this.state = {
            clr : ''
        }
    }

    componentWillMount(){
        let c;

        // some computations that change value of c based on props
        if( typeof ( this.props.game.one) == number ){
            c = 'Red'
        } else {
            c = 'Blue'
        }


        this.setState({
            clr : c
        })

    }

    render(){
        return(
            <span>{this.state.clr}</span>
        )
    }
}

On call of handle click method, there is a change in state of the game. 调用手柄单击方法时,游戏状态会发生变化。 However, subsequent changes in Child are not seen. 但是,看不到Child的后续更改。

Can anyone please help me out? 有人可以帮我吗? Where am I going wrong? 我要去哪里错了?

I'm not sure how to implement ref as suggested over here . 我不确定如何按照这里的建议实现ref。 However, I don't think that is a good solution. 但是,我认为这不是一个好的解决方案。 Probably I'm having some conceptual misunderstanding. 可能我在概念上有误解。

PS: Please ignore any syntax error. PS:请忽略任何语法错误。 This code is a strip down of real code. 此代码是真实代码的一部分。 If you want to take a look at full code - go over here 如果您想看完整的代码- 在这里查看

componentWillMount in your Piece component will only fire once. componentWillMountPiece组件才会触发一次。

Are you updating your state each time the props change as well ? 每次道具更改时,您是否也在更新状态? For example using componentWillReceiveProps 例如,使用componentWillReceiveProps

Also you could just display the props directly in your render function 您也可以直接在渲染功能中显示道具

render(){
    return(
        <span>{use the props directly here}</span>
    )
}

Try to avoid using state whenever possible, and stick with just rendering your component based on props. 尽量避免使用状态,并坚持仅基于props渲染组件。

it's a pretty basic react process: 这是一个非常基本的反应过程:

class Game extends Component{
    constructor(){
        super();
        this.state = {
            game :{ 
                 board_position = { 'One' : ''}
             }
        }

        this.handleClick = this.handleClick.bind(this);
    }

    handleClick(e){
        let p = this.state.game.board_position;

        // some computations on p
        p = { board_position : { 'One' : Math.random() } }

        this.setState({
            game : p
        })
    }

    render(){
        return(
        <div onClick={this.handleClick}>
            <Piece board_position={this.state.game.board_position} />
        </div>
        )
    }
}

Child: //can be a function instead of a class 子级://可以是函数而不是类

const Piece = ({board_position}) =>{
  const {board_position} = this.props;
  let clt = board_position;
  //do some stuff

  return(
    <span>{board_position}</span>
  );
};

if you want to do more calculations you can use componentWillReceiveProps(newProps) 如果要进行更多计算,可以使用componentWillReceiveProps(newProps)

A component re render when it's props or states get changed. 当组件的属性状态发生更改时,组件将重新呈现。 There will be certain life cycle methods get invoked based on the values of props and own states . 将根据props的值和自己的状态调用某些生命周期方法。

The way you accessed your **props is wrong.** It should be 您访问** props的方式是错误的。**应该是

this.props.One

Life cycle method you should have used is componentWillReciveProps(nextProps) . 您应该使用的生命周期方法是componentWillReciveProps(nextProps) In that case you should access the relevant prop like below 在这种情况下,您应该访问如下相关的道具

nextProps.One

I have created a working fiddle (your code had errors if you fixed those and looked at your log you could have easily figure out where the error is) 我创建了一个有效的提琴 (如果您修复了这些错误并查看了日志,您的代码就会出现错误,您可以很容易地找出错误在哪里)

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

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