D
D
Daniel Chistyakov2021-05-11 19:45:47
React
Daniel Chistyakov, 2021-05-11 19:45:47

Why is undefined when deriving a specific object from Mobx?

Good day, I'm trying to display information from Mobx, but when accessing a specific object - undefined, but if I access a global variable, then everything is fine. I suspect it's asynchronous, can anyone share a solution?
Link to CodeSandbox

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-05-11
@danielchistyakov

When you access a value, it doesn't exist yet.
Changes are not tracked.
DB class - you need to add a default value:

class DB {
  info = null;
  ...

The Episodes component - add the DB.info value as a dependency to the effect:
const Episodes = observer(() => {
  useEffect(() => {
    ...
  }, [ DB.info ]);
  ...

If you do not want to see the default info value inside Episodes, then its instance should be rendered only if there is data, in App replace with . return <Episodes />;return DB.info && <Episodes />;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question