简体   繁体   English

父道具在React / Redux中更新太晚

[英]Parent prop updating too late in React/Redux

I have a parent component (parent.js) that calls two children components (child1.js, child2.js). 我有一个父组件(parent.js),它调用了两个子组件(child1.js,child2.js)。 This is all on one tab - child1 is basically an input form to add an event on a calendar and child2 is the calendar. 所有这些都在一个选项卡上-child1基本上是在日历上添加事件的输入表单,而child2是日历​​。

Once I create the event from child1, the event doesn't render on the calendar until I click on another tab and then back to this one, or I refresh the page. 从child1创建事件后,该事件将不会显示在日历上,除非单击另一个选项卡,然后再返回到该选项卡,否则我将刷新页面。 My goal is that once the event gets created, the calendar should update automatically without having to click away. 我的目标是创建事件后,日历应自动更新,而无需单击。

The problem is, the prop that's being passed down to child2 (array of events) isn't updating on time. 问题是,传递给child2(事件数组)的道具没有及时更新。

This is a small portion of parent.js 这是parent.js的一小部分

const mapStateToProps = state => ({
  volunteers: selectors.volunteer.getAll(state)
})

changeState = () => {
  console.log("changing state in parent")
  this.setState({show: true})
}

render() => {
  <NewShift
    volunteers={this.props.volunteers}
    makeShift={this.props.makeShift}
  />
  <Index
    volunteers={this.props.volunteers}
  />
}

This is child1, this function is called when the user submits the event. 这是child1,当用户提交事件时将调用此函数。 It adds a shift to the database 它增加了对数据库的转换

  saveFood = () => {
    const volunteer = this.props.getVolunteer(this.state.formInputFields.id)
    this.props.makeShift({
      ...volunteer,
      shift: {
        role: volunteer.firstName,
        date: this.state.formInputFields.date,
        duration: 2,
        notes: this.state.formInputFields.notes
      }
    })
    this.props.changeState()
  }   

Finally, this is child2. 最后,这是child2。 I'm calling componentDidUpdate() to update the calendar, but this.props.volunteers does not include the newest event that was already successfully added to the database. 我正在调用componentDidUpdate()更新日历,但是this.props.volunteers不包括已经成功添加到数据库中的最新事件。

    componentDidUpdate() {

        console.log("Updated")
        const calendar = this.state.c

        /* Removes all events from calendar */
        const events = calendar.getEvents()
        for(var x = 0; x < events.length; x++) {
            calendar.getEventById(events[x].id).remove()
        }


        /* Re-renders all events on calendar */
        var volunteers = this.props.volunteers
        for(var i = 0; i < volunteers.length; i++) {
            var shift = volunteers[i].shift

            for(var j = 0; j < shift.length; j++) {
                calendar.addEvent({
                    id: j,
                    title: volunteers[i].firstName + ' ' + volunteers[i].lastName,
                    start: new Date(shift[j].date + 'T00:00:00'),
                    notes: shift[j].notes
                })
            }
        }
    }

I would look into componentDidUpdate() documentation . 我将研究componentDidUpdate() 文档 this function take 2 parameters componentDidUpdate( prevProps, prevState ) in this function you can compare this.props to prevProps . 此函数有2个参数componentDidUpdate( prevProps, prevState )在此函数中,您可以将this.propsprevProps进行比较。 If there is a change between the two then perform the logic to update the component. 如果两者之间有变化,则执行逻辑以更新组件。

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

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