25
loading...
This website collects cookies to deliver better user experience
props
. But an issue arises when a deeply nested child requires data from a component higher up in the tree. If we pass on the data through the props
, every single one of the children would be required to accept the data and pass it on to its child, leading to prop drilling, a terrible practice in the world of React.In a typical React application, data is passed top-down (parent to child) via props, but such usage can be cumbersome for certain types of props (e.g. locale preference, UI theme) that are required by many components within an application. Context provides a way to share values like these between components without having to explicitly pass a prop through every level of the tree.
Create the Context
const Context = createContext(MockData);
Create a Provider for the Context
const Parent = () => {
return (
<Context.Provider value={initialValue}>
<Children/>
</Context.Provider>
)
}
Consume the data in the Context
const Child = () => {
const contextData = useContext(Context);
// use the data
// ...
}
Redux is a predictable state container for JavaScript apps.
It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time-traveling debugger.
You can use Redux together with React, or with any other view library. It is tiny (2kB, including dependencies), but has a large ecosystem of addons available.
Create a Reducer
import { createSlice } from "@reduxjs/toolkit";
export const slice = createSlice({
name: "slice-name",
initialState: {
// ...
},
reducers: {
func01: (state) => {
// ...
},
}
});
export const { func01 } = slice.actions;
export default slice.reducer;
Configure the Store
import { configureStore } from "@reduxjs/toolkit";
import reducer from "./reducer";
export default configureStore({
reducer: {
reducer: reducer
}
});
Make the Store available for data consumption
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './App.jsx'
import store from './store';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
Use State or Dispatch Actions
import { useSelector, useDispatch } from 'react-redux';
import { func01 } from './redux/reducer';
const Component = () => {
const reducerState = useSelector((state) => state.reducer);
const dispatch = useDispatch();
const doSomething = () = > dispatch(func01)
return (
<>
{/* ... */}
</>
);
}
export default Component;
Context API | Redux |
---|---|
Built-in tool that ships with React | Additional installation Required, driving up the final bundle size |
Requires minimal Setup | Requires extensive setup to integrate it with a React Application |
Specifically designed for static data, that is not often refreshed or updated | Works like a charm with both static and dynamic data |
Adding new contexts requires creation from scratch | Easily extendible due to the ease of adding new data/actions after the initial setup |
Debugging can be hard in highly nested React Component Structure even with Dev Tool | Incredibly powerful Redux Dev Tools to ease debugging |
UI logic and State Management Logic are in the same component | Better code organization with separate UI logic and State Management Logic |
I am a beginner, how should I learn Front-End Web Dev?
Look into the following articles:
Would you mentor me?
Sorry, I am already under a lot of workload and would not have the time to mentor anyone.
Would you like to collaborate on our site?
As mentioned in the previous question, I am in a time crunch, so I would have to pass on such opportunities.