This tutorial will help to integrate redux-saga & redux-form in React Native app. You can also clone the github repo provided at the end of this guide.
2. 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.
6. Redux-Saga
Redux-Form
Register
Login
Dashboard
Field Array – Field Array component is
used for rendering an array of fields.
Wizard Form – The common pattern for
separating a single form into different
input pages is Wizard.
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:
7. 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.
9. 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 () => (
<ReduxProvider store={store}>
<App/>
</ReduxProvider>
)
10. Individual action.js and saga.js file for the
Register and Login Page (which we will see
in the coming sections)
A folder named store having three files –
index.js, reducers.js, and sagas.js
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];
14. RegisterPageOne.js
RegisterPageTwo.js
RegisterPageThree.js
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'
16. 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 (
<View>
<Field
name="fullName"
component={FormInput}
validate={[fullnameRequired]}
18. 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(
23. 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) =>
Register/RegisterPageThree.js
The RegisterPageThree component includes
two password fields. Added validation that
both passwords should match.
27. error: error.message});
}
}
export default function*
signupScreenSaga() {
yield takeLatest('REGISTER_REQUEST',
signupUser);
}
Explanation
On clicking submit button, onSubmit
will be called, and signupUser(payload)
will be dispatched containing payload.
dispatch(signupUser(payload))
28. From the file Register/action.js, the
Register action type
“REGISTER_REQUEST” will be
dispatched.
Then saga middleware will watch for
the “REGISTER_REQUEST” type action.
It will take the latest encounter of that
action and call the register API.
For a successful call, it will dispatch the
“REGISTER_SUCCESS” action with
response data.
For a Fail call, it will dispatch the
“REGISTER_FAILURE” action with an
error message.
30. 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
37. }
}
export default function* loginScreenSaga() {
yield takeLatest('LOGIN_REQUEST',
loginUser);
}
Explanation
On clicking the submit button, onSubmit
will be called, and loginUser(values) will
be dispatched containing the user email
and password.
const onSubmit = (values) =>
dispatch(loginUser(values))
38. From the file Login/action.js, the Login
action type “LOGIN_REQUEST” will be
dispatched.
Then saga middleware will watch for
the “LOGIN_REQUEST” type action.
It will take the latest encounter of that
action and call the login API.
For a successful call, it will dispatch the
“LOGIN_SUCCESS” action with
response data.
For a Fail call, it will dispatch the
“LOGIN_FAILURE” action with an error
message.
42. 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 (
<SafeAreaView style={styles.screen}>
<Text style={styles.hiText}>Hii,
there</Text>
Once you are successfully logged in, you’ll
be redirected to the dashboard page. Here’s
the code for the UI.
Dashboard/index.js
43. <Text style={styles.name}>{userEmail}
</Text>
</SafeAreaView>
);
};
export default DashBoard;
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.
45. 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.