N
N
Nikita2021-01-05 15:16:33
React
Nikita, 2021-01-05 15:16:33

Why doesn't the component re-render after the mobx state changes?

There is an Aside that should show/hide when you click a switcher

import { observer } from 'mobx-react'
import asideStore from './aside.store'

const Aside = observer(props => {
    const switcherClick = () => {
        asideStore.toggleAside()
    }

    return (
        <Wrapper {...props}>
            <Switcher onClick={switcherClick} />
            {asideStore.show ? 'show' : 'hide'}
        </Wrapper>
    )
})


And there is the simplest store for Aside
import { observable } from 'mobx'

class AsideStore {
    @observable show

    constructor() {
        this.show = true
    }

    toggleAside() {
        this.show = !this.show
    }
}

export default new AsideStore()


The problem is what this.showgets changed but not re-rendered in the component.

With what it can be connected?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bazyn, 2021-01-12
@Prynik

Seems like it should work.

@action.bound
 toggleAside() {
        this.show = !this.show
    }

https://mobx.js.org/actions.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question