简体   繁体   中英

React context not causing a rerender

Very puzzled by this situation. I have two methods in the context, one causes a rerender in the consumer just fine and the other doesnt.

This one works fine:

this.setPage = (tag) => {
            this.setState(state => {

                for (let item of tag) {
                    if (!state.tags.includes(item)) {
                        state.tags.push(item)
                    }
                }

                state.currentTags = tag
                return tag
            })
        }

but this one doesnt:

this.popPage = (tag) => {
            this.setState(state => {                
                if (state.tags.includes(tag)) {
                    if (state.tags.length == 1)
                        return

                    state.tags.pop()
                    state.currentTags = [state.tags[state.tags.length - 1]]

                    return tag
                }
            })
        }

These are basically just pushing and popping items into the array. The code runs and works fine, but there is no update in the consumer for the second function

<Page.Consumer>
   {page => /*Components go here*/}
</Page.Consumer>

First of all, you should return state (not tag). I don't know what tag is for exactly but to me it doesn't make much sense that next state is set as tag. In the second block, even worse. In case condition is not met, it even doesn't return anything as the next state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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