We have learned and implemented the basics of redux-form in our previous tutorial: Integrate Redux Form (Part-1). You can visit the tutorial if you don’t have a basic idea of Redux Form or are struggling to use it in your application. Now, it’s time to step up and introduce redux-saga with redux form. Are you looking for a simple tutorial to get started with redux middleware in your react native application? If yes, then you have chosen the correct blog!
In this tutorial, we will implement an authentication module in our demo application using redux-saga and redux form.
Before moving towards implementing our authentication module, watch the video below to have a better idea of the demo application.
Mainly we will learn about-
The demo will contain an Authentication module in which we have used redux-form. The module will have-
Use of two redux-form features:
And also called Firebase REST API from the Login and Register module.
So, we are clear with the requirements and what the demo will contain. Without wasting more time, let’s get started with building our demo application.
The very first step will be setting up the store.
For connecting the store to your application, open app/index.js
app/index.js
import { store } from 'store' import { Provider as ReduxProvider } from 'react-redux' export default () => ( )
So, now the store is accessible through our entire application. It’s time to use the store in our app. For that, we will need to do two things-
store/index. js
import createSagaMiddleware from 'redux-saga'; import {createStore, applyMiddleware} from 'redux'; import {combinedReducers} from './reducers'; import rootSaga from './sagas'; const sagaMiddleware = createSagaMiddleware(); const middlewares = [sagaMiddleware]; const store = createStore(combinedReducers, applyMiddleware(...middlewares)); sagaMiddleware.run(rootSaga); export {store};
store/reducers.js
import {combineReducers} from 'redux'; import {reducer as formReducer} from 'redux-form'; export const combinedReducers = combineReducers({ form: formReducer, Auth: AuthReducer, });
store/sagas.js
import {all} from 'redux-saga/effects'; import loginScreenSaga from 'screens/Login/saga'; import signupScreenSaga from 'screens/Register/saga'; function* rootSaga() { yield all([loginScreenSaga(), signupScreenSaga()]); } export default rootSaga;
We will separate the Register form into separate pages of inputs named-
This pattern of splitting is known as Wizard Form.
Register/index.js
To separate the form into small forms we will use a state variable- page for keeping track of the page to be rendered.
import React, { useState } from 'react’ import { ScrollView } from 'react-native' import RegisterPageOne from './RegisterPageOne' import RegisterPageTwo from './RegisterPageTwo' import RegisterPageThree from './RegisterPageThree' const Register = (props) => { const [page,setPage] = useState(1) const onSubmit = (Values) => console.log(Values) const goToNextPage = () => setPage( page => page + 1 ) const goToPrevPage = () => setPage( page => page - 1 ) return ( {page === 1 && ) } export default Register} {page === 2 && } {page === 3 && }
Register/RegisterPageOne.js
RegisterPageOne is our first component which will have two fields : Full Name and User Name.
import React from 'react' import { View } from 'react-native' import { Field, reduxForm } from 'redux-form' import { FormInput , CustomButton } from 'components' import { usernameRequired , fullnameRequired } from 'utils/Validations' const RegisterPageOne = ({handleSubmit,nextPage}) => { return ( ) } export default reduxForm({ form: 'register-form', destroyOnUnmount: false, forceUnregisterOnUnmount: true })(RegisterPageOne)
Register/RegisterPageTwo.js
RegisterPageTwo is our second component having three fields: Mobile No, Email, and Hobbies. The input for hobbies uses the FieldArray API to render multiple inputs to the user. We will also add validation to the input fields.
import React from 'react' import { StyleSheet, View , Text } from 'react-native' import { Field, FieldArray, reduxForm } from 'redux-form' import { DeleteButton, CustomButton, FormInput } from 'components' import { emailRequired, mobileNoRequired, validateEmail, validateMobileno } from 'utils/Validations' const renderHobbies = ({ fields, meta : {error , submitFailed } }) => { return( ) } const validate = (values,props) => { const errors = {} if (!values.hobbies || !values.hobbies.length) errors.hobbies = { _error : “Add at least One hobby” } else{ const hobbiesArrayErrors = [] values.hobbies.forEach((hobby,index)=>{ if (!hobby || !hobby.length) hobbiesArrayErrors[index] = HOBBY_REQUIRED }) if(hobbiesArrayErrors.length) errors.hobbies = hobbiesArrayErrors } return errors } const RegisterPageTwo = ({prevPage,handleSubmit,nextPage}) => { return ( fields.push({})} /> {fields.map((hobby,index) => ( ))} { submitFailed && error && fields.remove(index)} /> {error} }) } export default reduxForm({ form: 'register-form', validate, destroyOnUnmount : false, forceUnregisterOnUnmount : true })(RegisterPageTwo)
Register/RegisterPageThree.js
The RegisterPageThree component includes two password fields. Added validation that both passwords should match.
import React from 'react' import { View } from 'react-native' import { connect } from 'react-redux' import { Field, formValueSelector, reduxForm } from 'redux-form' import { CustomButton, FormInput } from 'components' import { passwordRequired, validatePassword } from 'utils/Validations' let RegisterPageThree = ({handleSubmit,onSubmit,prevPage}) => { const validateConfirmPassword = (password) => password && password !== props.password ? VALIDATE_CONFIRM_PASSWORD : undefined return ( ) } RegisterPageThree = reduxForm({ form: 'register-form', destroyOnUnmount : false, forceUnregisterOnUnmount : true })(RegisterPageThree) const selector = formValueSelector('register-form') export default RegisterPageThree = connect(state => { const password = selector(state, 'password') return {password} })(RegisterPageThree)
Register/action.js
Register/saga.js
import {put, takeLatest} from 'redux-saga/effects'; import {userSignup} from 'api/Signup'; function* signupUser({payload}) { try { const response = yield userSignup(payload); yield put({type: 'REGISTER_SUCCESS', response}); } catch (error) { yield put({type: 'REGISTER_FAILURE', error: error.message}); } } export default function* signupScreenSaga() { yield takeLatest('REGISTER_REQUEST', signupUser); }
Open store/reducers.js and add this chunk of code stating switch cases.
This reducer serves actions coming from Login and Register. Both the modules dispatch similar types of actions-
1. Request Action: Upon this action, the reducer updates the loading variable to true.
2. Success Action: Upon this action, the reducer updates the loading variable to false and stores the response from the action to the uservariable.
3. Failure Action: Upon this action, the reducer updates the loading variable to false and stores response coming from action to error variable
store/reducers.js
const AuthReducer = (state = initialState, action) => { switch (action.type) { case 'REGISTER_REQUEST': return { ...state, loading: true, }; case 'REGISTER_SUCCESS': return { ...state, user: action.response, loading: false, }; case 'REGISTER_FAILURE': return { ...state, error: action.error, loading: false, }; case 'LOGIN_REQUEST': return { ...state, loading: true, }; case 'LOGIN_SUCCESS': return { ...state, user: action.response, loading: false, }; case 'LOGIN_FAILURE': return { ...state, error: action.error, loading: false, }; default: return state; } };
The Login page accepts two inputs- email and password.
Login API is called on clicking onSubmit Button. After successful login Dashboard screen will appear.
Login/index.js
import React from 'react' import { Field, reduxForm } from 'redux-form' import { useDispatch } from 'react-redux' import { View, ScrollView } from 'react-native' import { loginUser } from './actions' import { FormInput, CustomButton } from 'components' import { passwordRequired, emailRequired, validatePassword , validateEmail } from 'utils/Validations' const Login = (props) => { const dispatch = useDispatch() const onSubmit = (values) => dispatch(loginUser(values)) return ( ) } export default reduxForm({ form: 'login-form' })(Login)
Login/action.js
Login/saga.js
import {put, takeLatest} from 'redux-saga/effects'; import {userLogin} from 'api/login'; function* loginUser({payload}) { try { const response = yield userLogin(payload); yield put({type: 'LOGIN_SUCCESS', response}); } catch (error) { yield put({type: 'LOGIN_FAILURE', error: error.message}); } } export default function* loginScreenSaga() { yield takeLatest('LOGIN_REQUEST', loginUser); }
api/login/index.js
const API_KEY = '' //put your key here const endpoint = { login : `https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${API_KEY}` } export const userLogin = async (user) => { const response = await fetch(endpoint.login,{ method: 'POST', headers: { 'Content-Type' : 'application/json' }, body: JSON.stringify({ email: user.email, password: user.password, returnSecureToken: true }) }) if(!response.ok) throw new Error('Something Went Wrong!!'); const responseData = await response.json() return responseData.email }
Once you are successfully logged in, you’ll be redirected to the dashboard page. Here’s the code for the UI.
Dashboard/index.js
import React from 'react'; import {useSelector} from 'react-redux'; import {scale, verticalScale} from 'react-native-size-matters'; import {Text, StyleSheet, SafeAreaView} from 'react-native'; import Colors from 'utils/Colors'; const DashBoard = () => { const userEmail = useSelector(state => state.Auth.user); return ( ); }; export default DashBoard; Hii, there {userEmail}
useSelector() allows you to extract data from the Redux store state, using a selector function, so that we can use it in our component. As you can see, we can get the value of the user’s email address from state.Login.user.
You can find the entire source code of the demo application at Github Repository, from where you can clone and play around with code.
So, this was about implementing redux-saga with your redux-form in React Native application. We have covered complete authentication using redux-form features Field Array, Wizard Form, and redux-middleware. You can visit the React Native Tutorials page for more such tutorials and clone the github repository to experiment.
If you are looking for a helping hand for your React Native application, please contact us today to hire React Native developers from us. We have experienced and skilled developers having fundamental and advanced knowledge of React Native.
Your Success Is Guaranteed !
We accelerate the release of digital product and guaranteed their success
We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.