Redux, how to access store state variables in functions

Asked

Viewed 164 times

0

Hi, my question is if I can access store variables in functions, something like:

--usuarioReducer.js--

const initialState = {
   usuario: {},
   isAuth: false
}

--root.js--
import { Redirect } from 'react-router-dom';

export default class App extends Component {
   componentDidMount() {
      if(!**variavel_is_auth_store**) { // Se isAuth === false, redireciona para Login
         return <Redirect to={'/Login'} />
      }
   }
}

I wonder if it is possible to take that variable of Reducer that is in the Store and use it in functions

  • has yes, that’s what it’s for.

  • Yeah, but how to do it?

  • your minimum code needs to update all code related to Redux ... only it becomes difficult to respond

  • a good study link: https://medium.com/reactbrasil/initiator-redux-c14ca7b7dcf

1 answer

0


As was said the idea of the React-Redux is the same. To access the store in your components you need to connect to the store and map the state of the store you need. This is done with the Redux connect which is an HOC that needs a function with the mapping configuration you need. Usually this mapped by standardization we call the function mapStateToProps. The documentation explains this well. But if you search the internet how to do mapStateToProps has a lot of content. This link details well, see: Mapping the state of the store

Browser other questions tagged

You are not signed in. Login or sign up in order to post.