Rafał Developer
Rafał Developer

Reputation: 2055

Observer global variables reactjs

Is it possible to observer global const for example gloabl const we can call this Auth.ts

I would like to do this https://mobx-react.js.org/observe-how but globaly

import { observable, action, computed } from 'mobx';

const userAutehnticationObserver = observable({
  loggedIn: false,
  tokenId: ''
})

and from another tsx function

import React from "react";

    const Dashboard = () => {
    const P1 = observer(function P1({ userAutehnticationObserver }) {
      return <h1>{person.name}</h1>
    })
      return (
        <div>
        </div>
      );
    };
    export default Dashboard;

and third function change something onClick to first global const which is observable by this second function

Upvotes: 0

Views: 1015

Answers (1)

brayanarrieta
brayanarrieta

Reputation: 149

Yes, of course, you can do that basically you inject the store in the different components that you need, that in the case that you need to change something can use an action to produce a change in the global store. That will be reflected in all the component that has injected the store and use the observer

Upvotes: 1

Related Questions