W
W
Worddoc2019-11-13 18:56:49
React
Worddoc, 2019-11-13 18:56:49

Mobx: how to render container passing observable from store to child component?

Mobx only renders an observer component when an observable structure is accessed in that component. Those. if, split the component into a Container and a Presentation Component, the Container is not rendered, so the Presentation Component does not get updated data.

import React, { Component } from 'react';
import { observer, inject } from 'mobx-react';

import TodoView from './Todo-view';

@inject('TasksModel')
@observer
class Todo extends Component {
    render() {
        const {
            TasksModel: { tasks },
        } = this.props;
        return (
            <TodoView tasks={tasks} />
        );
    }
}

export default Todo;

But if you formally refer to the observable structure inside the render method of the Container, for example, through tasks.length;, then it will consider that the observable is used, and the observer will re-render the container.
The question is: how to let the container re-render, if in render they will only be used by passing them to the child component?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-11-13
@Worddoc

Add @observer to TodoView
Better yet, @inject there as well.
And throw out your Todo.
And don't mix all the concepts you've come across on the internet in one code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question