Skip to content
This repository was archived by the owner on Dec 31, 2020. It is now read-only.

Mobx-React does not work with React.CreateContext #471

@jantimon

Description

@jantimon

Versions

Newest versions as of May 2018

mobx: 4.2.1
mobx-react: 5.1.2
react: 16.3.2
react-dom: 16.3.2

Bug

It seems like it is not possible to create an observing context consumer.

// A simple store which counts up the count value:
class CounterStore {
  @observable count: number = 0;
}
const counterStore = new CounterStore();
setInterval(() => counterStore.count ++, 1000);

// A simple context for the store
const Context = React.createContext<CounterStore>(null);

Expected behaviour

All context.consumers which rely on an observed value should rerender once an observed value changes.

Actual behaviour

The following screenshot contains two components which rely on the same observable value.
The first component does not access the observed value the second one does.
As you can see the first component does not rerender.

react-ts-q5ktj1_-_stackblitz

Demo

The following demos show two scenarios for the same problem:

ReactContext which holds a mobx store
https://stackblitz.com/edit/react-ts-q5ktj1?file=index.tsx

ReactContext which does not hold a mobx store
https://stackblitz.com/edit/react-ts-rjk13r?file=index.tsx

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions