Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React/Redux - dispatch action on app load/init

Tags:

reactjs

redux

I have token authentication from a server, so when my Redux app is loaded initially I need make a request to this server to check whether user is authenticated or not, and if yes I should get token.

I have found that using Redux core INIT actions is not recommended, so how can I dispatch an action, before app is rendered?

like image 929
Shota Avatar asked Jul 25 '16 09:07

Shota


People also ask

Is it possible to dispatch an action without using mapDispatchToProps?

Without mapDispatchToPropsNotice that the component receives a dispatch prop, which comes from connect() , and then has to use it directly to trigger the actions.

Who is responsible for dispatching the action in Redux?

A Redux app really only has one reducer function: the "root reducer" function that you will pass to createStore later on. That one root reducer function is responsible for handling all of the actions that are dispatched, and calculating what the entire new state result should be every time.

How do you bind action creators with dispatch function?

bindActionCreators(actionCreators, dispatch) Turns an object whose values are action creators, into an object with the same keys, but with every action creator wrapped into a dispatch call so they may be invoked directly. Normally you should just call dispatch directly on your Store instance.

How do I dispatch an action in React Redux?

dispatch is a function of the Redux store. You call store.dispatch to dispatch an action. This is the only way to trigger a state change. With React Redux, your components never access the store directly - connect does it for you. React Redux gives you two ways to let components dispatch actions:

What is the use of Dispatch in Redux?

dispatch is a function of the Redux store. You call store.dispatch to dispatch an action. This is the only way to trigger a state change. With React Redux, your components never access the store directly - connect does it for you. React Redux gives you two ways to let components dispatch actions:

How do I trigger a state change in React Redux?

dispatch is a function of the Redux store. You call store.dispatch to dispatch an action. This is the only way to trigger a state change. With React Redux, your components never access the store directly - connect does it for you.

What is Redux in react?

In Javascript, Redux is used as state container for many apps, mostly with React. It relies on actions that get dispatched and listened by reducers, which update the state accordingly. In this guide, we'll look at different ways of dispatching the action and at isolating the components from Redux.


Video Answer


1 Answers

You can dispatch an action in Root componentDidMount method and in render method you can verify auth status.

Something like this:

class App extends Component {   componentDidMount() {     this.props.getAuth()   }    render() {     return this.props.isReady       ? <div> ready </div>       : <div>not ready</div>   } }  const mapStateToProps = (state) => ({   isReady: state.isReady, })  const mapDispatchToProps = {   getAuth, }  export default connect(mapStateToProps, mapDispatchToProps)(App) 
like image 116
Serhii Baraniuk Avatar answered Oct 09 '22 13:10

Serhii Baraniuk