【问题标题】:Why does child component do not re-render after prop change?为什么子组件在 prop 更改后不重新渲染?
【发布时间】:2019-12-27 13:16:41
【问题描述】:

我有一个父组件,它从数组中呈现一个 corousel(子组件)中的列表。当我尝试在事件上重新呈现列表时,只有子组件中的道具会发生变化,而不是不会在 corousel 中重新呈现新列表的状态。

从下拉菜单中,我选择要呈现的列表,onclick 会更改子组件的 props。

export default class DetailsInfo extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            showText: false,
            bannerObj:{},
            selectedid : 0,
            selectedSeason:props.item.contentType==='WebSeries'?props.item.season[0].name:'',
            seasonObj:[{
                    "id": 1,
                    "name": "Session 1",
                    "parts": [
                      {
                        "id": 1,
                        "name": "abcc",
                        "discription": null,
                        "url": "http://www.demo.com"
                      },
                      {
                        "id": 2,
                        "name": "abcdd",
                        "discription": null,
                        "url": "http://www.demo.com"
                      },
                      {
                        "id": 3,
                        "name": "abcddd",
                        "discription": null,
                        "url": "http://www.demo.com"
                      }
                    ]
                  },
                {
                    "id": 2,
                    "name": "Session 2",
                    "parts": [
                      {
                        "id": 1,
                        "name": "se2ofe1",
                        "discription": null,
                        "url": "http://www.demo.com"
                      },
                      {
                        "id": 2,
                        "name": "se2ofe2",
                        "discription": null,
                        "url": "http://www.demo.com"
                      }
                    ]
                  }
            ]
        };
        this.handlechange = this.handlechange.bind(this);
    }

    handleOnDragStart = e => e.preventDefault()

    handlechange(event){
        const vid = this.state.seasonObj.find(o=>o.name===event.target.value).id;
        this.setState({
            selectedSeason : event.target.value,
            selectedid : vid-1
        })
    }


    render() {

        return (
            <Grid>


                <div className="maindiv">
                <div>
                        <Select onChange={this.handlechange} value={this.state.selectedSeason}>
                            {this.state.seasonObj.map(item => 
                                <MenuItem key={item.id} value={item.name} id={item.id}>{item.name}</MenuItem>
                            )}
                        </Select>
//this is were the selected id is passed to the props to child
                        {this.state.seasonObj[this.state.selectedid].id &&
                            <SliderItem item={this.state.seasonObj[this.state.selectedid].parts} />
                        }

                        </div>
                </div>
            </Grid>
        )
    }
}

// child class
export default class SliderItem extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            list: props.item
        }

    }

    render() {

        return (
            <React.Fragment>

                <AliceCarousel>
                    {this.state.list
                        .map((item) =>
                            <div key={item.id} >
                              <img alt={item.title} src={item.url}/>
                        </div>)}
                </AliceCarousel>
            </React.Fragment>
        )
    }
}

从下拉列表中选择时,我需要更新 courosel 列表。

【问题讨论】:

    标签: reactjs setstate


    【解决方案1】:

    如果你想重新渲染它,你必须在你的 AliceCarouse 中使用道具:

    <AliceCarousel>
      {this.props.item.map((item) => (
        <div key={item.id}>
          <img alt={item.title} src={item.url} />
        </div>
      ))}
    </AliceCarousel>;
    

    【讨论】:

    • AliceCarousel 是一个反应库,所以它会接受其他参数作为道具吗?我还在努力...谢谢
    • 当我更新父组件中的道具时,我看不到子组件状态的变化。为什么会发生这种情况,无法与其他文章相关联,因为它到处都说当父组件更改道具时,子组件将被重新渲染? @maksym bezruchko
    • 在构造函数方法中,您只需从 props 值初始化您的状态。如果您希望每次道具更改时都重新渲染子组件,则不需要这样做。您可以在这里阅读更多内容:reactjs.org/docs/components-and-props.html 此外,如果由于某种原因您需要在每次道具更改时更改您的状态,您可以使用 componentDidUpdate 方法。
    • 当父组件改变 props 子组件的 props 时是。但它不会改变状态,因为 props 被分配给构造函数中的状态,为了反映你应该在 componentWillReceiveProps 中将该值设置为 state 的变化
    • AliceCarousel 还需要一个“items”道具,其中包含滑过所需的所有数据,通过道具传递这些数据!谢谢大家!!
    猜你喜欢
    • 2020-08-29
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    • 2018-07-05
    • 2021-12-15
    相关资源
    最近更新 更多