Hackernoon logoSmart Strategy To Create Login Function in React Native Using Powerful Tools by@skd1993

Smart Strategy To Create Login Function in React Native Using Powerful Tools

Shobhit Hacker Noon profile picture


Front-end developer | Business Architect | DC Fan

In this post, we will see how an action can be dispatched using Redux on login, and set the app state accordingly.

Assuming familiarity with React Native and Redux concepts

We have the following Login component to begin with :

import React, { useState } from 'react';
import { View, Button, Text } from 'react-native';

import CustomButton from '../../components/CustomButton';
import InputField from '../../components/InputField';

import { styles } from './style';

const Login = (props) => {

  // remove these initial assignments after testing
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');

  return (
        placeholder='Enter username'
        onChangeText={(text) => setUsername(text)}
        placeholder='Enter password'
        onChangeText={(text) => setPassword(text)}
        title='Sign In' 
        onPress={() => }  

export default Login;

Right now it doesn’t do anything, it’s just barebones UI.

To make it “react” to user action we should update the

paramter in the

<CustomButton title='Sign In' onPress={() => } />

We use

here, so pressing the button should dispatch and action to the reducer which should in turn update the overall app's state.

For sake of simplicity, all redux code is placed in a ‘

’ folder, while the components are in ‘

This is how our

folder looks like.
├── actions.js
├── actionTypes.js
├── initialState.js
├── reducer.js
└── store.js

Let’s set

as follows. These are all the fields that our login API will return (yours may differ).


in are flags that we will set on our own (these are not part of API response)

export const initialState = {
  isLoggedIn: false,
  userId: '',
  token: '',
  refreshToken: '',
  expiresOn: '',
  data: '',

Define action type in



Let’s now create our


import { initialState } from './initialState';
import * as t from './actionTypes';

export const loginReducer = (state = initialState, action) => {
  switch (action.type) {
    case t.SET_LOGIN_STATE:
      return {
        ...action.payload, // this is what we expect to get back from API call and login page input
        isLoggedIn: true, // we set this as true on login
      return state;

We can now generate our redux store using all the available information and thunk as middle-ware to handle API calls.

import thunkMiddleware from 'redux-thunk';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly'; // this is for debugging with React-Native-Debugger, you may leave it out
import { loginReducer } from './reducer';

const rootReducer = combineReducers({
  loginReducer: loginReducer,

export const store = createStore(

We have these things in place but we still have not figured how to set the state from the Login component. For this we need to define some actions in


What we are looking at is a function that can call the login API and return the result back to us.

Something like :

return fetch(LoginUrl, {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      body: JSON.stringify(loginInput),

But we also need to ensure that the action is “connected” to the “reducer” in order to update the

state or store.

Since API call is considered unpredictable, it should not dispatch the action object directly to reducer, but through a helper.

Dispatching action can return only an Object. If it returns a promise the app will break. We need to make sure of this.



import * as t from './actionTypes';
import { LoginUrl } from '../constants/Api';

// this is what our action should look like which dispatches the "payload" to reducer
const setLoginState = (loginData) => {
  return {
    type: t.SET_LOGIN_STATE,
    payload: loginData,

To fetch this

in the action above, we create another function using the fetch operation discussed above:

import { Alert } from 'react-native'; // to show alerts in app

export const login = (loginInput) => {
  const { username, password } = loginInput;
  return (dispatch) => {  // don't forget to use dispatch here!
    return fetch(LoginUrl, {
      method: 'POST',
      headers: {  // these could be different for your API call
        Accept: 'application/json',
        'Content-Type': 'application/json',
      body: JSON.stringify(loginInput),
      .then((response) => response.json())
      .then((json) => {
        if (json.msg === 'success') { // response success checking logic could differ
          dispatch(setLoginState({ ...json, userId: username })); // our action is called here
        } else {
          Alert.alert('Login Failed', 'Username or Password is incorrect');
      .catch((err) => {
        Alert.alert('Login Failed', 'Some error occured, please retry');

You can see how our action is dispatched from this function, which in turn will return a payload object to the reducer in order to perform state update.

Only thing remaining now is connecting this function to the UI. Let’s go back to our

component in the Submit button section and specify

import { useDispatch } from 'react-redux';
import { login } from '../../redux/actions';


        title='Sign In' 
        onPress={() => useDispatch(login({'username': username, 'password': password }))}  

Since we are using

here, all our functions should ideally be in form of some action, which will be caught in the
middleware first and then passed on appropriately to reducer.

On successful login, the

values will all be populated. On failure, an alert will show up stating error.

I hope this was helpful & informative :)

Also published at https://dev.to/skd1993/creating-a-simple-login-function-with-redux-and-thunk-in-react-native-33ib


Join Hacker Noon

Create your free account to unlock your custom reading experience.