简体   繁体   English

React 原生子组件没有从父母 state 获得 componentWillReceiveProps

[英]React native child component did not get componentWillReceiveProps from parents state

I'm trying to implement timer that counts the next event from the JSON API.我正在尝试实现从 JSON API 计算下一个事件的计时器。 Unfortunately the API does not support filter so I have to process it on the component不幸的是 API 不支持过滤器,所以我必须在组件上处理它

here's the code:这是代码:

constructor(props) {
    super(props);

    this.scrollViewRef = React.createRef();

    this.state = {
        agendaVisible: false,
        scrollOffset: null,
        markedDates: {},
        dateString: Date(),
        agendaItems: {},
        nextEvent: {
            start: 99999999999,
            name: "",
        },
        isProcessing: true,
    };

    this.closeModal = this.closeModal.bind(this);
    this.openModal = this.openModal.bind(this);
}

setMarkedDates = () => {
    const { events } = this.props;

    if (events !== undefined) {
        Object.keys(events).map((key) => {
            let now = Date.now();
            let difference = events[key].start * 1000 - now;
            let a = new Date(events[key].start * 1000);
            let date = `${a.getUTCFullYear()}-${this.str_pad(
                a.getUTCMonth() + 1
            )}-${this.str_pad(a.getUTCDate())}`;

            if (difference > 0 && difference < this.state.nextEvent.start) {
                this.setState({
                    nextEvent: {
                        start: events[key].start * 1000,
                        name: events[key].name,
                    },
                });

                console.log("Goes in here: " + events[key].start);
            }
            }
            // Set Marked dates on calendar and agenda Items
            this.setState(({ markedDates, agendaItems }) => ({
                markedDates: {
                    ...markedDates,
                    [date]: {
                        marked: true,
                        selectedColor: "blue",
                        activeOpacity: 0,
                        dotColor: "blue",
                    },
                },
                agendaItems: {
                    ...agendaItems,
                    [date]: [
                        {
                            name: events[key].name,
                            height: 100,
                        },
                    ],
                },
            }));
        });

        this.setState({ isFetching: false });
    }
};

Then I passed in the nextEvent to my EventTimer component in the render然后我将 nextEvent 传递给渲染中的 EventTimer 组件

            {!this.state.isFetching && (
                <EventTimer
                    nextEvent={this.state.nextEvent}
                    nextEventStart={this.state.nextEventStart}
                />
            )}

Now the problem is, whenever I called {this.props.nextEvent.start} in the render, it works, however I need to set it up the state of eventTimer for static endTime to calculate the difference every interval, I put the code on componentWillReceiveProps but I never get the updated props?现在的问题是,每当我在渲染中调用{this.props.nextEvent.start}时,它都可以工作,但是我需要为 static endTime 设置 eventTimer 的 state 来计算每个间隔的差异,我把代码放在componentWillReceiveProps 但我从来没有得到更新的道具? it still stays at 9999999999, what happened?它仍然停留在 9999999999,发生了什么?

If it doesn't receive the props then which part of the lifecycle received the props?如果它没有收到道具,那么生命周期的哪一部分收到了道具? Because it works when I tried to render it.因为当我尝试渲染它时它可以工作。 I'm not sure where should I update the state from the props anymore.我不确定我应该在哪里更新道具中的 state。

Please help, Thanks!请帮忙,谢谢!

componentWillReceiveProps(nextProps) {
    this.setState({
        endTime: Date.now() + this.props.nextEvent.start,
    });

    this.resetTimer();
}

resetTimer = () => {
    if (this.interval) clearInterval(this.interval);
    this.interval = setInterval(
        this.setTimeRemaining,
        this.timeRemaining % 1000
    );
};

You are referring to the initial props in the componentWillReciveProps .您指的是componentWillReciveProps中的初始道具。

this.setState({
    // this.props refers to the current props. 
    // endTime: Date.now() + this.props.nextEvent.start,

    endTime: Date.now() + nextProps.nextEvent.start,
});

While that should fix the bug you are facing.虽然这应该可以解决您面临的错误。 You should try to not use this lifecycle method and instead depend on a calculated value from the parent component.您应该尽量不使用这种生命周期方法,而是依赖于父组件的计算值。

The componentWillReciveProps will be deprecated in the next major version. componentWillReciveProps将在下一个主要版本中被弃用。 You can read a bit more about this in the react docs.您可以在react docs 中阅读更多有关此内容的信息。

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

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