If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In "React Intermediate - Class 4," we'll continue exploring more intermediate React concepts and build on what we learned in the previous classes. We'll cover topics such as managing state with Redux and handling asynchronous actions.
Step 1: Introduction to Redux
Redux is a predictable state container for JavaScript apps. It helps manage the state of your application in a centralized store, making it easier to manage and modify application state.
Install Redux:
To get started with Redux, you need to install the required packages:
bashCopy code
npm install redux react-redux
Step 2: Creating a Redux Store
Let's create a simple Redux store for managing the theme state from the previous classes.
Create a new file called
themeReducer.js
inside thesrc
folder with the following content:jsxCopy code
const themeReducer = (state = 'light', action) => { switch (action.type) { case 'TOGGLE_THEME': return state === 'light' ? 'dark' : 'light'; default: return state; } }; export default themeReducer;
Step 3: Combining Reducers
In larger applications, you might have multiple reducers to manage different parts of the state. You can use the
combineReducers
function from Redux to combine these reducers into a single root reducer.jsxCopy code
import { combineReducers } from 'redux'; import themeReducer from './themeReducer'; const rootReducer = combineReducers({ theme: themeReducer, }); export default rootReducer;
Step 4: Creating the Redux Store
Now, create the Redux store and provide it to the entire application using the
Provider
component fromreact-redux
.Create a new file called
store.js
inside thesrc
folder with the following content:jsxCopy code
import { createStore } from 'redux'; import rootReducer from './reducers/rootReducer'; const store = createStore(rootReducer); export default store;
Step 5: Dispatching Actions
To update the state in the Redux store, we need to dispatch actions. Actions are plain JavaScript objects that describe what happened in the application.
Create a new file called
themeActions.js
inside thesrc
folder with the following content:jsxCopy code
export const toggleTheme = () => { return { type: 'TOGGLE_THEME', }; };
Step 6: Using Redux in Components
Now, let's use Redux in the
App.js
andButton.js
components.In
Button.js
, use Redux'suseDispatch
Hook to dispatch thetoggleTheme
action:jsxCopy code
import React from 'react'; import { useDispatch } from 'react-redux'; import { toggleTheme } from './actions/themeActions'; function Button({ text }) { const dispatch = useDispatch(); const handleClick = () => { dispatch(toggleTheme()); }; return ( <button onClick={handleClick} style={{ padding: '10px 20px', fontSize: '16px' }}> {text} </button> ); } export default Button;
In
App.js
, wrap the entire application with theProvider
component and pass the Redux store as a prop:jsxCopy code
import React from 'react'; import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom'; import { Provider } from 'react-redux'; import Home from './Home'; import About from './About'; import Form from './Form'; import ParentComponent from './ParentComponent'; import UserList from './UserList'; import Button from './Button'; import { ThemeProvider, ThemeContext } from './ThemeContext'; import Counter from './Counter'; import useCounter from './useCounter'; import store from './store'; function App() { const { theme, setTheme } = React.useContext(ThemeContext); const { count, increment, decrement } = useCounter(0); const toggleTheme = () => { setTheme((prevTheme) => (prevTheme === 'light' ? 'dark' : 'light')); }; return ( <Router> <Provider store={store}> <ThemeProvider> <div style={{ backgroundColor: theme === 'light' ? '#fff' : '#333' }}> <h1>Hello, React!</h1> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/about">About</Link> </li> <li> <Link to="/products">Products</Link> </li> </ul> </nav> <Switch> <Route exact path="/" component={Home} /> <Route path="/about" component={About} /> <Route path="/products" component={Products} /> <Route path="/form" component={Form} /> <Route path="/user-list" component={UserList} /> <Route path="/parent-component" component={ParentComponent} /> <Route path="/button"> <Button text="Toggle Theme" /> </Route> <Route path="/counter"> <Counter count={count} increment={increment} decrement={decrement} /> </Route> </Switch> <Button onClick={toggleTheme} text="Toggle Theme" /> </div> </ThemeProvider> </Provider> </Router> ); } export default App;
Congratulations! You've completed the fourth class of React Intermediate. You've learned about managing state with Redux, dispatching actions, and using Redux in components.
React and Redux together provide a powerful and flexible way to build scalable and maintainable applications. As you continue your React journey, you can explore more advanced topics such as middleware in Redux, Redux thunk for handling asynchronous actions, and integrating Redux with React Router.
Keep practicing, building more complex applications, and exploring the vast possibilities of React and Redux! Happy coding!
Comments: 24
Kindly check the lecture number 6. Lifecycle of a Change in 1. Git and Github The above lecture is not related to the course please update it ASAP Thank you
From where can I download the files which love babbar says he has uploaded on dashboard ??
I have attached the link https://github.com/lakshayk12/ANN_optimization_BTP
bro, i paid for the course, but still can't access the course. do something
Paid for the course but it still locked, can solve this problem please, when you take the payment the couse should be unlocked, evey time i have to message for any purchase
Now your Paypal payment is accepted and your course is activated successfully. Please leave your valuable feedback.
Why i am being asked for payment if i am already enrolled in the course
Dear anand, your payment is now updated check your course, apologize for several delays. Please leave your valuable feedback.
thanks its working now
hello ,I have a doubt
hello i have a doubt
hi
I'm paying through my Card, but it giving an error. why ? how we can purchase a course in Pakistan. Paypal is banned here
can we download the videos
i done my payment for mern stack development love babbar but cant acces to course kindly give me access
Dear Krishnapal your payment is already accepted go and check back.
Why i am being asked for payment if i am already enrolled in the course
Dear Shivansh, if you already done your payment then contact us on our official Email id [email protected] or reply here.
Why i am being asked for payment if i am already enrolled in the course
Dear p8354046, if you already done your payment then contact us on our official Email id [email protected] or reply here.
Why i am being asked for payment if i am already enrolled in the course
Dear, Don't worry now your problem is solved check your id.
Why i am being asked for payment if i am already enrolled in the course
Yes bro
I'm facing same issue
Dear, Don't worry now your problem is solved check your id.
i want debit card option
how can i assure that this is not scam?
i have same doubt is this website real or a fraud
No its not any fraud. its genuine if you need any proof please contact us via email.
No it's not any scam, its genuine if you need any proof please contact us via email.
how to do payment ?
Click on Watch now button then click next video then payment option is showing
i am unable to access the video even i have already completed my payment .
Your problem is solved
plz add codes lecture wise ! it wold be great and helpful also
now we have added starter package in lecture 2nd, go now and download it.
how to view this one
Why video not play
Now it's working.
Thank you for the access of the videos. It would be greatful if I could no the way to download the videos
Use idm to download
ok
Hello bro can you share course with me?
hello everyone