React State Management: Do This, Not That A common mistake I often see in React codebases is allowing child components to directly manipulate the parent’s state. Problematic Approach const [state, setState] = useState(); // setState(state + 1) inside child component This approach can lead to: Tight coupling between components Reduced reusability Harder debugging and maintenance ✅ Recommended Approach // Parent component const [state, setState] = useState(); // Pass callback to child <Child toggle={() => setState(prev => !prev)} /> In this pattern: The state remains owned by the parent The child component receives a callback function Components stay decoupled and easier to maintain Key Principle: State should live in the component that owns it, while child components interact with it through callbacks. Bonus Tip Prefer functional updates like: setState(prev => !prev) This helps prevent stale state issues and ensures updates are always based on the latest value. Small architectural decisions like this can significantly improve the scalability and maintainability of React applications. #ReactJS #WebDevelopment #JavaScript #FrontendDevelopment #CleanCode #Programming
React State Management: Avoid Direct Manipulation
More Relevant Posts
-
A senior dev once reviewed my React app and said: "Your code is clean. But you're punishing your users." I had no idea what he meant — until he showed me the bundle size. 5.2MB of JavaScript. Loaded all at once. Every. Single. Visit. That day I learned: ✂️ Code Splitting — break JS into small chunks, load only what's needed 🌳 Tree Shaking — strip out dead code before it ever reaches the browser I went from 5MB → 900KB. Load time dropped from 11s → 2s. I turned those lessons into a simple visual carousel with code examples. Swipe to learn both 👉 —— 💾 Save this before your next PR. ♻️ Share with a dev who ships slow bundles. #JavaScript #React #WebDev #FrontendDevelopment #Performance #Programming
To view or add a comment, sign in
-
## First Round of Interview Questions — Frontend React 1. What challenges did you face while migrating from React version 17 to React version 18? 2. What is Redux Thunk, and why did you choose not to use Redux Saga? 3. What is asynchronous programming in JavaScript, and how do you handle it? 4. What are Promises in JavaScript? 5. What is a custom Hook in React? Write the code for a custom Hook. 6. What are Hooks in React? List out all the Hooks you know. 7. What is `React.memo()`? 8. What are `useCallback()` and `useMemo()`? 9. What is a Higher-Order Component (HOC)? List out the HOCs in React. 10. What are Error Boundaries in React? 11. What testing libraries are used in React? 12. What is Micro Frontend architecture? Explain how Module Federation works. 13. Difference between Axios and Fetch? 14. How do you optimize React application performance?
To view or add a comment, sign in
-
Building My Own Reusable Star Rating Component in React As part of my journey learning React, I decided to stop relying on external npm packages for simple UI features (For security and Reliability reasons) and instead build them from scratch. One of the components I built recently is a fully reusable Star Rating component Features I implemented: - Configurable number of stars (max) - Controlled and uncontrolled rating support - Click interaction to set rating - Custom colors support (border, fill, shadow) - Reset / clear rating option - Callback function triggered when the user selects a rating, passing the updated rating value as an argument , this is helpfull for external handling (e.g: state sync with parent components) - Fully reusable design #React #JavaScript #FrontendDevelopment #WebDevelopment #LearningInPublic #Programming #UI #CleanCode
To view or add a comment, sign in
-
JavaScript can only do one thing at a time. So how does it wait for data without freezing? That's where async/await comes in. → async before a function — it now returns a Promise → await pauses the function and waits for the result → Your app doesn't freeze — only that function waits → Always use try/catch — errors break your app silently without it → Two independent calls? Use Promise.all — don't await one by one → async/await is built on Promises — not a replacement Clean code. Easy to read. Fewer bugs. What part of async code confused you first? 👇 #javascript #asyncawait #webdevelopment #nodejs #programming #javascripttips #frontend #learnjavascript #100daysofcode
To view or add a comment, sign in
-
🚀 React Redux vs Redux Toolkit — Which One Should You Use? If you're working with state management in React, you've probably come across both React Redux and Redux Toolkit. 🔹 React Redux ✔️ More manual setup ✔️ Requires writing boilerplate code ✔️ Full control over configuration ✔️ Good for understanding core Redux concepts 🔹 Redux Toolkit ✔️ Simplified and faster setup ✔️ Built-in features (like createSlice, configureStore) ✔️ Less boilerplate code ✔️ Recommended by the Redux team #ReactJS #Redux #ReduxToolkit #WebDevelopment #FrontendDevelopment #JavaScript #Coding #DeveloperJourney
To view or add a comment, sign in
-
-
🚨 Most React developers overcomplicate state management After working on multiple projects, I noticed a pattern: Developers jump into tools like Redux, Zustand, or RTK Query too early. But the truth is 👇 👉 70% of your state can be handled with just useState + useReducer Here’s what actually matters: ✅ Keep state close to where it’s used ✅ Avoid global state unless absolutely necessary ✅ Split components instead of adding more logic ✅ Derive state instead of duplicating it Simple example Instead of this: global store multiple selectors unnecessary re-renders You can often do: 👉 local state + props 👉 or a small custom hook 💡 The real skill in React is not adding tools it’s knowing when NOT to use them Curious - what’s the most overused React tool in your opinion? #react #reactnative #frontend #javascript #webdevelopment #programming #softwareengineering #reactjs #cleanCode #developer
To view or add a comment, sign in
-
-
🚀 Understanding `useState` & `useEffect` in React If you're working with React, these two hooks are must-know fundamentals: 🔹 **useState** * Used to create and manage state inside a functional component * Returns a state value and a setter function * Triggers re-render when state changes Example: ```js const [count, setCount] = useState(0); ``` 🔹 **useEffect** * Used for side effects (API calls, subscriptions, DOM updates) * Runs after the component renders * Can depend on state or props Example: ```js useEffect(() => { console.log("Component mounted or count changed"); }, [count]); ``` 💡 **Why `useState` should be declared before `useEffect`?** React hooks follow a strict rule: 👉 Hooks must be called in the same order on every render. Since `useEffect` often depends on state values, defining `useState` first ensures: * State is initialized before being used * Dependencies inside `useEffect` are available * Hook order remains consistent (avoiding bugs or crashes) ⚠️ Breaking hook order can lead to unexpected behavior and hard-to-debug issues. ✅ Always follow: 1. Declare state (`useState`) 2. Then handle side effects (`useEffect`) --- Mastering these basics makes your React apps more predictable and maintainable 💻✨ #React #JavaScript #WebDevelopment #Frontend #Programming #ReactHooks
To view or add a comment, sign in
-
A closure isn't a concept to memorize. It's something your code is already doing. Most developers hear the word "closure" and immediately feel behind. They're not. They've been writing closures since day one. Take a look at the image below. That inner function has no variable of its own — but it remembers the one from the outer scope, even after the outer function has finished running. Not a copy. A live reference. Which is why the value keeps updating across calls instead of resetting to zero. That's the whole mechanism. That's a closure. You've already used this pattern without knowing the name: → Debounce and throttle utilities → Event handlers that track state between calls → React's useState — built on this exact idea The concept was never the hard part. The word made it sound harder than it is. Once you see it — you'll start spotting closures in code you wrote years ago. #JavaScript #WebDev #FrontendDevelopment #ReactJS #Programming
To view or add a comment, sign in
-
-
The Future of React: Embracing the React Compiler For years, `useMemo` and `useCallback` have been essential tools for optimizing React applications, helping developers manage performance by preventing unnecessary re-renders. However, as we move into 2026, the landscape of React development is undergoing a significant transformation with the increasing adoption and standardization of the React Compiler. This paradigm shift means that manual memoization, once a best practice, is becoming less critical. The React Compiler intelligently optimizes your components at build time, effectively eliminating the need for developers to manually wrap functions and values. This not only leads to cleaner, more readable code but also frees up development time to focus on core logic and user experience. What does this mean for developers? • Simplified Codebase: Say goodbye to the boilerplate of `useMemo` and `useCallback`, resulting in more concise and maintainable components. • Automatic Performance Gains: The compiler handles optimizations automatically, ensuring your applications run efficiently without constant manual intervention. • Enhanced Developer Experience: Focus on writing functional code, knowing that performance concerns are being addressed under the hood. This evolution marks a pivotal moment for the React ecosystem, promising a future where performance optimization is seamlessly integrated into the development workflow. Are you ready to embrace a cleaner, more efficient way of building with React? #ReactJS #WebDevelopment #FrontendDevelopment #ReactCompiler #CodingBestPractices #DeveloperExperience #JavaScript #TechTrends #SoftwareEngineering
To view or add a comment, sign in
-
-
💡 Lesson Learned Today: While working on implementing search functionality in a React + Redux application, I faced an issue where the search input was updating correctly… but the results were not filtered at all 🤔 After debugging, I discovered an important insight: 👉 Not every “search issue” comes from the frontend. ✔️ The request was being sent correctly ✔️ The search parameter was included in the API call ❌ But the backend endpoint didn’t support filtering with that parameter 🔍 What I learned: Always verify: Is the correct parameter name being sent? Is the backend actually using it? Check the Network tab before assuming the issue 🚀 Sometimes the best debugging skill is knowing where the problem is NOT. #FrontendDevelopment #React #Redux #Debugging #WebDevelopment #LearningJourney
To view or add a comment, sign in
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development