While developing a React Application, Redux is an integral part which comes into play at least at an intermediate level and is considered to be an essential part of the development process. Without going into much detail of what Redux is, let’s go on with a brief introduction of what Redux Structure is and what all components are needed to understand its architecture.
Related Read: React useEffect Hooks: A Guide For Beginners
State in redux controls the complete behaviour of components within react native. These are mutable i.e. change over time. An example for the same could be updating a text on the click of a button. In this case, the text updation will happen by a change in-state only.
import React, { useState } from 'react'; import { Text, View, Button } from 'react-native'; function App() { // Declare a new state variable, which we'll call "user" const [user, setUser] = useState(‘John’); return ( <View> <Text>Welcome {name} !</Text> <Button onPress={() => setUser(‘Peter’)}> title = {“Update”} </Button> </View> ); }
As already clear with the name, in general terms, it can be considered as an act to bring about a change.
Here, An action is a plain JavaScript object (notice how we don’t introduce any magic?) that describes what happened. An example of action can be a click on the button within the screen. Actions send data from let’s say API calls to the store. Those actions have a property called “type” that describes the type of action and also the data to be sent to the store.
A code snippet for the same can be:
function loginAction(form) { return { type: LOGIN_SUBMIT, payload: {username: ‘John’, password: ‘12345678’} } }
It can be considered as a function that binds together a state and an action of a component and returns back the next state i.e. the updated state for the same component.
Step 1: An action at first occurs within a component, such as someone trying to log in, list some data, see the details of a component, or even delete a component, etc.
Step 2: Which then calls the action and action type
Step 3: That then calls the particular reducer that will update or modify the state
Step 4: And once the state is changed, the view is rerendered
Now that we have a brief description of components which will be used in Redux Structure. Let’s go ahead and get started with our Redux Structure.
In this method, all the four components in use are created in different files as shown:
This is considered as the traditional method now as if any functionality needs to be changed, the changes have to be done in all the four files.
You’d have to edit the constants in one file, then edit the reducer in 2nd, then edit the actions in 3rd one, and finally, edit the action creators in yet another.
This means even for adding a small feature, might require adding and editing different files which is quite cumbersome and time-consuming! 😥
Let’s take an example for this approach:
Create a directory for the constants within redux: src/redux/constants
Create a new action-types.js file within that folder. In that file:
// src/redux/constants/action-types.js ... export const LOGIN_SUBMIT = “LOGIN_SUBMIT”;
Then open up src/redux/actions/index.js and update the action to use action types:
// src/redux/actions/loginAction.js import { LOGIN_SUBMIT} from “../constants/action-types”; export const loginsubmit = data => ({type: LOGIN_SUBMIT, payload: data });
In the example, let’s consider a feature called “Login”. The redux structure is shown below where all the files related to a particular feature are kept in a folder. As can be seen, the files are still the same as the above approach but now encapsulated within a folder. Summarizing –
//src/login - login (folder) - loginComponent.js (screen) - loginReducer.js - loginActions.js - loginConstants.js
Ducks is essentially a method where reducers, actions and action types all are bundled into the same file irrespective of the feature.
This method basically resolves the issue of hopping back and forth within the files when organized by the above 2 methods. With this approach we can combine all in one package and create modules. So let’s go ahead and refractor the above code.
So we created a basic functionality of login above and now we’ll need to move action_types, actions and reducers all in a single file rather than 3-4 different files.
Step 1: Create a directory for the Ducks within redux:
src/redux/ducks
Step 2: Create a file index.js within that folder
Step 3: Take action_types from the constants folder and put it into new ducks file i.e. in index.js
// src/redux/ducks/index.js ... export const actionTypes = { loginSubmit: { LOGIN_SUBMIT = "LOGIN_SUBMIT" } };
Step 4: Now delete the entire constants folder created earlier.
Step 5: Next take the actions file created earlier and put it under ducks file.
// src/redux/ducks/index.js … export const actions = { loginSubmit = data => ({type: LOGIN_SUBMIT, payload: data }) };
Step 6: Continue and let’s delete the actions file now!
Step 7: Lastly get hold of the reducer file and begin the change! In the ducks file add:
// src/redux/ducks/index.js … export const rootReducer = (state = initialState, action) => { switch (action.type) { case LOGIN_SUBMIT : return {...state, loginResponse: [...state.loginResponse, action.payload] }; default: return state; } };
Step 8: Now, of course, let’s delete the reducer folder!
Hooray! We’ve just created a modified Ducks file! Let’s take a look.
The above file works the same way but now has been reformed for a clean and robust look with an optimized management. Moreover, it’s pretty easy to create!
As far as structure is considered every developer has their own methodology. Choose the one you like and start. There might be plenty of methods available like this. Hope this article helps to avoid mess and restructure yourself along with the code.
Keep Spreading Knowledge and Happy Coding!! 🙂🙂
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowThe Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork