💡React Tip💡 Never pass the setState function directly as a prop to any of the child components like this: const Parent = () => { const [state, setState] = useState({ name: '', age: '' }) . . . return ( <Child setState={setState} /> ) } ✅ The state of a component should only be changed by that component itself. ✅ This ensures your code is predictable. If you pass setState directly to multiple components, it will be difficult to identify from where the state is getting changed. ✅ This lack of predictability can lead to unexpected behavior and make debugging code difficult. ✅ Over time, as your application grows, you may need to refactor or change how the state is managed in the parent component. ✅ If sensitive data is part of the state, directly passing setState could potentially expose that data to child components, increasing security risks. ✅ React's component reconciliation algorithm works more efficiently when state and props updates are clearly defined within components. Instead of passing setState directly, you can do the following: 1️⃣ Pass data as prop: Pass the data that the child component needs as props, not the setState function itself. This way, you provide a clear interface for the child component to receive data without exposing the implementation details of state. 2️⃣ Pass function as prop: If the child component needs to interact with the parent component's state, you can pass the function as a prop. Declare a function in the parent component and update the state in that function, you can pass this function as a prop to the child component and call it from the child component when needed. 𝗧𝗵𝗶𝘀 𝗶𝘀 𝘁𝗵𝗲 𝗿𝗲𝗮𝘀𝗼𝗻, 𝘄𝗵𝗶𝗹𝗲 𝗹𝗶𝗳𝘁𝗶𝗻𝗴 𝘀𝘁𝗮𝘁𝗲 𝘂𝗽, 𝘄𝗲 𝗱𝗼𝗻'𝘁 𝗱𝗶𝗿𝗲𝗰𝘁𝗹𝘆 𝗽𝗮𝘀𝘀 𝘀𝗲𝘁𝗦𝘁𝗮𝘁𝗲 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝘁𝗼 𝗰𝗵𝗶𝗹𝗱 𝗰𝗼𝗺𝗽𝗼𝗻𝗲𝗻𝘁𝘀, 𝗶𝗻𝘀𝘁𝗲𝗮𝗱 𝘄𝗲 𝗱𝗲𝗰𝗹𝗮𝗿𝗲 𝗮 𝗻𝗲𝘄 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝗶𝗻 𝗽𝗮𝗿𝗲𝗻𝘁 𝗮𝗻𝗱 𝗽𝗮𝘀𝘀 𝘁𝗵𝗮𝘁 𝗮𝘀 𝗮 𝗽𝗿𝗼𝗽 𝘁𝗼 𝘁𝗵𝗲 𝗰𝗵𝗶𝗹𝗱 𝗰𝗼𝗺𝗽𝗼𝗻𝗲𝗻𝘁. 𝘍𝘰𝘳 𝘮𝘰𝘳𝘦 𝘴𝘶𝘤𝘩 𝘶𝘴𝘦𝘧𝘶𝘭 𝘤𝘰𝘯𝘵𝘦𝘯𝘵, 𝘥𝘰𝘯'𝘵 𝘧𝘰𝘳𝘨𝘦𝘵 𝘵𝘰 𝘧𝘰𝘭𝘭𝘰𝘸 𝘮𝘦. 𝗣𝗦: If you found this tip helpful, you'll definitely love my new 𝗥𝗲𝗮𝗰𝘁 𝟱𝟬+ 𝗣𝗿𝗼 𝗧𝗶𝗽𝘀 𝗘𝗯𝗼𝗼𝗸 packed with many more such practical React tips. 𝗟𝗶𝗻𝗸 𝗶𝗻 𝘁𝗵𝗲 𝗰𝗼𝗺𝗺𝗲𝗻𝘁 𝗮𝗻𝗱 𝗶𝗻 𝘁𝗵𝗲 𝗳𝗲𝗮𝘁𝘂𝗿𝗲𝗱 𝘀𝗲𝗰𝘁𝗶𝗼𝗻 𝗼𝗳 𝗺𝘆 𝗟𝗶𝗻𝗸𝗲𝗱𝗜𝗻 𝗽𝗿𝗼𝗳𝗶𝗹𝗲. #javascript #reactjs #webdevelopment
Prevent Direct setState Passing in React Components
More Relevant Posts
-
🚨 This JavaScript Bug Wasted Hours of My Time (So You Don’t Have To) I thought my code was safe. I wrapped it with try/catch. Still… my app kept crashing. I spent way more time than I want to admit debugging this 👇 try { setTimeout(() => { throw new Error("Boom"); }, 1000); } catch (err) { console.log("Caught:", err); } Nothing was caught. No logs. Just silent failure. 😤 🤯 What I finally understood • try/catch catches only synchronous errors • setTimeout runs asynchronously • When the error happens, try/catch is already gone JavaScript didn’t break. My understanding did. ✅ The fix that actually works ----------------------------------------------- setTimeout(() => { try { throw new Error("Boom"); } catch (err) { console.log("Caught:", err); } }); Or the cleaner async pattern 👇 async function run() { try { await asyncTask(); } catch (err) { console.error(err); } } 🧠 The lesson I learned (the hard way) try/catch is not a global shield. Async code needs async thinking. Once I understood this: • Debugging became faster • Logs started making sense • Production felt less scary 💬 If you’re reading this… You’re either: 1️⃣ About to face this bug 2️⃣ Already faced it 3️⃣ Or will face it soon If this saves you even 30 minutes, it was worth posting. Drop a 🔥 if you’ve hit this before Save it — future you will thank you #JavaScript #NodeJS #Debugging #WebDevelopment #DeveloperLife
To view or add a comment, sign in
-
-
A JavaScript Quirk That Still Breaks Apps 🚨 This JavaScript Line Looks Innocent… But It Breaks Apps typeof null === "object" Yes. This is true 😐 And no — it’s not a feature. It’s JavaScript’s oldest mistake. 🤯 Why this happens Back in the early days of JavaScript, null was stored as a zero pointer. JavaScript never fixed it… because fixing it would break the internet 🌍 💥 Real-world bug example if (typeof data === "object") { process(data); } 💣 This also runs for null 💣 App crashes silently 💣 Debugging takes hours ✅ The correct check if (data !== null && typeof data === "object") { process(data); } 🧠 Takeaway JavaScript isn’t weird. It’s backward-compatible. Knowing these quirks is what separates: 👉 beginners 👉 from confident developers 👉 Follow me for daily JavaScript clarity 🚀 #JavaScript #WebDevelopment #Coding #Frontend #Developers
To view or add a comment, sign in
-
-
🚀 React 19 is officially changing the game! 🚀 If you want to write cleaner, faster, and more efficient code, you need to look at these new features. From the new React Compiler to powerful new hooks, the DX (Developer Experience) just got a massive upgrade. Here is a breakdown of the most impactful features in React 19: ⚡ Key Highlights: React Compiler: Say goodbye to manual optimization. React now automatically tracks dependencies and optimizes components, eliminating the need for useMemo, useCallback, or memo. The New use() Hook: A versatile tool for data fetching and consuming async values like context or promises directly within your render. Server Components: Native integration that provides better SEO and improved performance by handling logic on the server. useOptimistic: Update your UI instantly with predicted results while waiting for the server to respond—making your apps feel lightning-fast. Cleaner Transitions: With useTransition, you can perform non-urgent updates to keep your UI responsive even during heavy tasks. Simplified Form Handling: New hooks like useFormStatus (to show loaders/disable buttons) and useActionState (to track async form results) make forms a breeze. ref is now a Prop: No more forwardRef! You can now pass ref directly like any other prop. .Provider is gone: You can now use context directly as the provider (e.g., <ThemeContext value="dark">). Level up your code with React 19! 💻✨ Which feature are you most excited to use in your next project? Let's discuss in the comments! 👇 #ReactJS #WebDevelopment #React19 #Frontend #Coding #Javascript #Programming #WebDev #ReactCompiler
To view or add a comment, sign in
-
Stop memoizing everything in React. You're probably making your app slower, not faster. 🎯 We've all been there. We read about React performance best practices and start wrapping everything: 💭 The thought process: "I'm being proactive about performance!" 🚫 Reality: This component is now slower than without memoization. Here's what most developers don't realize: memo(), useMemo(), and useCallback() aren't free. Every time your component renders, React has to: - Run comparison logic to check if props/dependencies changed - Store previous values in memory - Execute the equality checks For a simple component like the one above that renders in microseconds, the memoization overhead is often MORE expensive than just letting the component re-render naturally. When memoization actually helps: ✅ Components with expensive render logic (complex calculations, large lists) ✅ Heavy component trees that cascade re-renders ✅ Stabilizing references for dependency arrays in useEffect ✅ Preventing expensive child components from re-rendering When it's premature optimization: ❌ Simple components with primitive props ❌ Components that render quickly anyway ❌ "Just in case" memoization without profiling first The React 19 game-changer: Here's the exciting part - React 19 introduces the React Compiler, which changes everything. The compiler automatically analyzes your code and applies memoization where it actually helps, without you having to think about it. No more manual useMemo, useCallback, or memo() in most cases. The compiler handles it intelligently, only optimizing where there's a real benefit. My approach: 1. Build first - Write clean, readable code 2. Profile when needed - Use React DevTools when you notice actual performance issues 3. Optimize with data - Fix the specific bottlenecks you identify, not theoretical ones Remember: React is already fast by default. Premature optimization adds cognitive overhead to your code and often delivers zero performance benefit. 💡 The best optimization is the one you don't have to write. What's your experience with React memoization? Have you caught yourself over-optimizing? 💭 #React #WebDevelopment #JavaScript #Performance #ReactJS #React19
To view or add a comment, sign in
-
-
🚨 5 JavaScript Mistakes Most Developers Make (Early On) I made these mistakes. You probably did too. And that’s okay — learning happens here 👇 ❌ 1. Thinking JavaScript is “easy” JS looks simple… until: async bugs appear this behaves weird closures confuse you 👉 Simplicity ≠ weakness. ❌ 2. Ignoring the Event Loop If you don’t understand how async works, you’ll never fully debug JavaScript apps. Promises, async/await, callbacks — they all depend on the Event Loop. ❌ 3. Mutating objects accidentally const user = { name: "Alex" }; const copy = user; copy.name = "John"; Boom 💥 Original object changed. 👉 Reference vs Value matters more than you think. ❌ 4. Jumping to frameworks too fast React won’t fix weak JavaScript. Strong JS fundamentals = every framework feels easier. ❌ 5. Copy-pasting without understanding It works today. Breaks tomorrow. You’re stuck debugging someone else’s logic. Everyone makes mistakes. Good developers learn from them. 👉 Follow me for daily JavaScript & dev lessons 🚀 #JavaScript #WebDevelopment #Frontend #Developers #Coding #Learning
To view or add a comment, sign in
-
-
⚠️ Frameworks Don’t Make You a Good Developer It’s tempting to think learning the latest framework will fix everything. It won’t. What actually makes you good at web development: ✔️ Understanding HTML semantics ✔️ Knowing how CSS really works ✔️ Strong JavaScript fundamentals ✔️ Ability to debug and think logically Frameworks like Vue or React are just tools. If your foundation is weak, the tool won’t save you — it will only hide the problem for a while. I’ve seen developers know three frameworks but struggle with basic DOM logic. Master the basics. Frameworks will come and go — fundamentals stay. #WebDevelopment #JavaScript #Frontend #ProgrammingAdvice #LearnToCode
To view or add a comment, sign in
-
-
React19 introduces useActionState to standardize this entire flow 👇 . Handling form submissions in React used to be a ritual of boilerplate. We had to: 1. e.preventDefault() 2. Create useState for loading. 3. Create useState for errors. 4. Wrap the fetch in a try/catch block. ❌ The Old Way (Event Handlers): You manually manage the transition from "submitting" to "success" or "error." It’s imperative code that is easy to mess up (e.g., forgetting to reset the error state on the next attempt). ✅ The Modern Way (Action State): You pass an async function (Action) to the hook. React gives you: • state: The return value of the last action (e.g., validation errors or success message). • formAction: The function to pass to <form action={...}>. • isPending: A boolean telling you if the action is currently running. Why this is cleaner: 🧠 Declarative: You define what happens, not how to track the lifecycle. 🛡️ Progressive Enhancement: Works even if JavaScript hasn't loaded yet (if using a framework supporting SSR). 📉 Less Code: Deletes the onSubmit boilerplate entirely. Note: In previous Canary versions, this was called useFormState. It has been renamed to useActionState in the stable release. #ReactJS #React19 #WebDevelopment #Frontend #JavaScript #CleanCode #SoftwareEngineering #TechTips #Hooks #Tips #ReactDev #JS #ReactForm #ReactTips #FrontendDeveloper
To view or add a comment, sign in
-
-
React19 introduces useActionState to standardize this entire flow 👇 . Handling form submissions in React used to be a ritual of boilerplate. We had to: 1. e.preventDefault() 2. Create useState for loading. 3. Create useState for errors. 4. Wrap the fetch in a try/catch block. ❌ The Old Way (Event Handlers): You manually manage the transition from "submitting" to "success" or "error." It’s imperative code that is easy to mess up (e.g., forgetting to reset the error state on the next attempt). ✅ The Modern Way (Action State): You pass an async function (Action) to the hook. React gives you: • state: The return value of the last action (e.g., validation errors or success message). • formAction: The function to pass to <form action={...}>. • isPending: A boolean telling you if the action is currently running. Why this is cleaner: 🧠 Declarative: You define what happens, not how to track the lifecycle. 🛡️ Progressive Enhancement: Works even if JavaScript hasn't loaded yet (if using a framework supporting SSR). 📉 Less Code: Deletes the onSubmit boilerplate entirely. Note: In previous Canary versions, this was called useFormState. It has been renamed to useActionState in the stable release. #ReactJS #React19 #WebDevelopment #Frontend #JavaScript #CleanCode #SoftwareEngineering #TechTips #Hooks #Tips #ReactDev #JS #ReactForm #ReactTips #FrontendDeveloper
To view or add a comment, sign in
-
-
🧠 𝐎𝐧𝐞 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐜𝐨𝐧𝐜𝐞𝐩𝐭 𝐬𝐢𝐥𝐞𝐧𝐭𝐥𝐲 𝐝𝐞𝐜𝐢𝐝𝐞𝐬 𝐰𝐡𝐞𝐭𝐡𝐞𝐫 𝐲𝐨𝐮𝐫 𝐚𝐩𝐩 𝐢𝐬 𝐬𝐭𝐚𝐛𝐥𝐞 𝐨𝐫 𝐟𝐫𝐚𝐠𝐢𝐥𝐞. 👉 No errors. 👉 No warnings. 👉Just data changing where it shouldn’t. 𝐓𝐡𝐚𝐭 𝐜𝐨𝐧𝐜𝐞𝐩𝐭 𝐢𝐬 𝐡𝐨𝐰 𝐜𝐨𝐩𝐲𝐢𝐧𝐠 𝐰𝐨𝐫𝐤𝐬 𝐢𝐧 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭. If you don’t clearly understand 𝐝𝐞𝐞𝐩 𝐯𝐬 𝐬𝐡𝐚𝐥𝐥𝐨𝐰 𝐜𝐨𝐩𝐲, you’ll eventually: ✅ mutate state by accident ✅ break React updates ✅ introduce bugs that are hard to trace ✅ lose confidence in your own logic I put together a clear, example-driven PDF that explains this topic from the ground up, including: 1️⃣Primitive vs reference behavior 2️⃣ Shallow copy — what it protects and what it doesn’t 3️⃣ Deep copy — when and why it’s necessary 4️⃣ Safe patterns for real-world React state updates 5️⃣ Common mistakes that cause silent bugs This isn’t about syntax. It’s about thinking correctly about data in JavaScript. 📄 𝙋𝘿𝙁 𝙖𝙩𝙩𝙖𝙘𝙝𝙚𝙙 — 𝙙𝙚𝙨𝙞𝙜𝙣𝙚𝙙 𝙩𝙤 𝙨𝙩𝙧𝙚𝙣𝙜𝙩𝙝𝙚𝙣 𝙮𝙤𝙪𝙧 𝙛𝙪𝙣𝙙𝙖𝙢𝙚𝙣𝙩𝙖𝙡𝙨 𝙖𝙣𝙙 𝙞𝙢𝙥𝙧𝙤𝙫𝙚 𝙝𝙤𝙬 𝙮𝙤𝙪 𝙬𝙧𝙞𝙩𝙚 𝙘𝙤𝙙𝙚. #JavaScript #FrontendDevelopment #ReactJS #WebDev #ProgrammingFundamentals #Developers
To view or add a comment, sign in
-
Simpler code is better. So, you're building apps with React - and let's be real, who doesn't love the idea of keeping their code clean and easy to read? But have you ever stopped to think about just how many imports you're dealing with? It's crazy, right? You're importing libraries just to toggle a CSS class, and before you know it, your code is looking cluttered. What if I told you there's a way to make your code cleaner, without all those extra imports? You can use a new way of writing JSX that's more straightforward. For instance, take a look at this example: you can write a Card component with just a few lines of code, and it's actually pretty elegant. It's all about using the language to your advantage - and in this case, that means using JSX in a way that feels more natural. You define your Card component, and then you're using a simple div with some conditional classes, like 'border-blue-500' if it's active, or 'opacity-50 cursor-not-allowed' if it's disabled. And the best part? You don't need any extra imports to make it work. To get started, you just need to tell your compiler to use the new runtime - which is as simple as adding a couple of lines to your tsconfig.json or jsconfig.json file. You add "jsx": "react-jsx" to your compiler options, and then you add "jsxImportSource": "clsx-react". And that's it - you're good to go. You can even use this approach with Vite or Babel, which is pretty cool. The benefits are clear: you're removing unnecessary imports, you're getting rid of visual clutter in your JSX, and you're getting back to writing simple, straightforward code. It's a game-changer, trust me. So why not give it a try and see how it works for you? Source: https://lnkd.in/gSs6398r Optional learning community: https://lnkd.in/geSQUpYM #React #JSX #CleanCode
To view or add a comment, sign in
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
Especially, it's not easy to find out from where the state is getting updated, good info Yogesh Chavan