Urgent.News

What's breaking now, across thousands of outlets.

Tech

Props Drilling in React

When building React applications, we use props to pass data from one component to another. But what happens when we need to pass data through multiple components, even when some of those components don't need that data? This is called Props Drilling . Let's understand it with a simple example. 1. What is Props Drilling? Props Drilling happens when we pass data from a parent component to a deeply…

Props Drilling is a common challenge in React applications when transmitting data from a parent component to a deeply nested child component through multiple intermediate components. This occurs when data is passed through props to reach a component that actually requires it, even if some intermediate components don't need that data.

Consider an example with three components: App, Profile, and User. The App component has a username prop, and the User component needs to display it. However, the User component is nested inside the Profile component. To pass the data, the App component sends the username to the Profile component, which then passes it to the User component. The final output displays "Welcome, Lokesh".

This process of passing data through multiple components is aptly named Props Drilling. While it may be acceptable for small applications, it can lead to several issues in larger applications. These include more code, difficulty in maintaining data flow, and unnecessary props being passed to components that don't use them.

To avoid Props Drilling, React offers a solution called Context API. With Context API, data can be shared between components without the need to pass props through every intermediate component. To utilize Context API, first, create a context using createContext and then use the Provider to provide the data to intermediate components. The child component can then access the shared data using the useContext hook.

For instance, in the provided example, Profile doesn't need to receive or pass the username anymore. The User component can directly access the username using useContext(). This eliminates the need for Props Drilling, making the application more manageable and efficient.

Context API can be beneficial for sharing data across multiple components, such as in cases of Dark Mode, Authentication, Shopping Cart, or Language Settings. However, it's not suitable for every situation. For simple parent-to-child communication, traditional props are usually sufficient.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Browser Image Upload to Object Storage: 4 Retention Checks for Logistics Exports

Browser Image Upload to Object Storage: 4 Retention Checks for Logistics Exports Short answer: use a private bucket, let the server create a tenant-bound object key, and give the browser a presigned…

  • Utilize private bucket with tenant-bound object key and presigned upload URL.
  • Implement Cloudflare R2 lifecycle semantics with caution.
  • Shared REST paths preferred for simplicity and credential management.

GitHub Actions Workflow Lockfile: Pin Transitive Deps

GitHub just made workflow lockfiles generally available, and the gh actions-lock command finally answers a question that SHA pinning never could: what about the actions your actions call?

  • GitHub introduces workflow lockfiles to pin transitive dependencies precisely.
  • Lockfile complements YAML by recording exact commits for actions and dependencies.
  • Recommend starting with low-risk workflows before expanding to sensitive deploy workflows.

More from Sunday 15 November →