Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React & Redux : simple async login

I'm a beginner with React & Redux and I'm trying to set up a very simple login form & redirection. I'll add react-router or react-router-redux later.

I don't really understand where i have to put my 'logic code' (an ajax call and a redirection).

Here is what I've write.

index.js (entry point) :

import React from 'react'
import { render } from 'react-dom'
import { createStore } from 'redux'
import { Provider } from 'react-redux'
import App from './containers/App'
import rootReducer from './reducers/reducers'

let store = createStore(rootReducer);
let rootElement = document.getElementById('root');

render(
   <Provider store={store}>
      <App />
   </Provider>,
   rootElement
);

containers/App.js :

import { Component } from 'react'
import { connect } from 'react-redux'
import { login } from '../actions/actions'
import LoginForm from '../components/LoginForm'

class App extends Component {
   render () {
      const { dispatch } = this.props;

      return (
         <div>
            <LoginForm onSubmit={(id, pass) =>
               dispatch(login(id, pass))
            } />
         </div>
      )
   }
}

const mapStateToProps = (state) => {
   return {

   }
};
const mapDispatchToProps = (dispatch) => {
   return {

   }
};
export default connect(mapStateToProps)(App);

components/LoginForm.js :

import { Component, PropTypes } from 'react'

class LoginForm extends Component {
   render () {
      return (
         <div>
            <form action="#" onSubmit={(e) => this.handleSubmit(e)}>
               <input type="text" ref={node => { this.login = node }} />
               <input type="password" ref={node => { this.password = node }} />
               <input type="submit" value="Login" />
            </form>
         </div>
      )
   }

   handleSubmit(e) {
      e.preventDefault();
      this.props.onSubmit(this.login.value, this.password.value);
   }
}

LoginForm.propTypes = {
   onSubmit: PropTypes.func.isRequired
};

export default LoginForm;

reducers/root.js :

import { combineReducers } from 'redux'
import user from './user'

const rootReducer = combineReducers({
   user
});

export default rootReducer;

reducers/user.js :

import { LOGIN, BAD_LOGIN, LOGOUT } from '../actions/actions'

const initialState = {
   cid: null,
   username: '',
   logo: ''
};

const user = (state = initialState, action) => {
   switch (action.type) {
      case LOGIN:
         const api = new loginApi; //simple version
         api.login(action.login, action.password)
            .done(res => {
               //Right here ?
            })
            .fail(err => console.error(err));

         return state;

      case LOGOUT:
         //...
         return state;

      default:
         return state;
   }
};

export default user;

actions/actions.js :

export const LOGIN = 'LOGIN';
export const LOGOUT = 'LOGOUT';

export function login(login, password) {
   return {
      type: LOGIN,
      login,
      password
   }
}

following this link : http://redux.js.org/docs/advanced/AsyncActions.html I hesitate between write my login stuff inside the reducer (but I think reducer's purpose is just to reduce the state object) or to create multiple actions with one 'main' action which call REQUEST_LOGIN and LOGIN_SUCCES / LOGIN_FAILURE for example.

Thanks.

like image 578
Armelias Avatar asked Jan 25 '16 11:01

Armelias


People also ask

What is React is used for?

The React. js framework is an open-source JavaScript framework and library developed by Facebook. It's used for building interactive user interfaces and web applications quickly and efficiently with significantly less code than you would with vanilla JavaScript.

Is React for HTML or JS?

To get an overview of what React is, you can write React code directly in HTML. But in order to use React in production, you need npm and Node.js installed.

Is React Java or JavaScript?

React is a JavaScript-based UI development library. Facebook and an open-source developer community run it. Although React is a library rather than a language, it is widely used in web development. The library first appeared in May 2013 and is now one of the most commonly used frontend libraries for web development.

Is React for frontend?

React JS, commonly referred to as React is one frontend development framework, or to be more specific a library that has found a favorite with developers around the world.


1 Answers

You are correct, reducers are only for mapping data to the state. Create your async logic in the action creator. The key is to use a store enhancer to make async actions possible.

  • redux-thunk
  • redux-promise

A tutorial on async redux can be found in the redux documentation.

like image 101
Herku Avatar answered Sep 23 '22 14:09

Herku