Access state anywhere in react

how can I have a state in react which can be accessed anywhere ,like I have to check if admin has logged in if admin is logged in then show him certain features that are not visible to other users

If you want something to have access to the state of another object it has to be passed as props

1 Like

Well, the short answer is to use context API.

1 Like

yeah but ,I need to pass that to other component
cant pass it as props

yeah,I kinda know that but finding other way around

To access state anywhere in react, use global state managers like react-redux or context.

1 Like

yeah,but finding other way around cuz project is not that big

If project is small you have to use props or maybe try using global variables outside all the components. You can put all the components within one file to experiment the behaviour then modularize it as you see fit.

If project is not that big, then try to make a global state in app component then pass it through as a props to all this childrens.

I think he is trying to avoid passing props

I’m not exactly sure what you are looking for but if you search github for ‘react global state’ or ‘react global context’ you might find something there?

1 Like

guess I need to use context cuz

I’ve to see if admin is log in or not

show which user is logged in so that I can show data according to that

yes, context is a good choice

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.