Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between require() and passing an object via prop or context

What's the difference between require()ing a singleton, and passing it down via React prop or context?

What are the use cases for each?

like image 980
user1164937 Avatar asked Aug 23 '16 22:08

user1164937


People also ask

What's the main difference between props and state?

Props are used to pass data from one component to another. The state is a local data storage that is local to the component only and cannot be passed to other components.

What is the difference between props and state in React component?

What's the difference between state and props in React? In a React component, props are variables passed to it by its parent component. State on the other hand is still variables, but directly initialized and managed by the component.

How do you pass an object as a prop in React?

Use the spread syntax (...) to pass an object as props to a React component, e.g. <Person {... obj} /> . The spread syntax will unpack all of the properties of the object and pass them as props to the specified component. Copied!


1 Answers

If you require() (or import) a module, you get the same object in any component.
Use modules for the code your component depends on:

  • Other components (Button).
  • Utility functions (getTextColor).
  • Global data storage (CommentStore).

The upside of importing a module is that it’s extremely easy to do, and you only need to do it once.

The downside of importing a module is that you can’t override what it points to. So, for example, you can’t easily swap out CommentStore or getTextColor modules in testing, or in a “living design guide”. Depending on your use case, this may or may not be a problem.


Passing something as a prop means you can pass a different thing every time.
Use props for inputs to your components that need to be customizable:

  • Data (comment).
  • Event callbacks (onClick).
  • UI properties (color).

The upside of using props is that they’re explicit and customizable. They’re primary mechanism of passing data down in React so when in doubt, use props.

The downside of using props is that sometimes you might end up passing a lot of props through intermediate components that don’t use them, just to get props down to the leafs. Normally this is fine in React because it trades some verbosity for ease of finding bugs. But in some cases it can get frustrating.


Context is an advanced API and should be used very sparingly.
It is likely that it will significantly change in the future.

Context is like implicitly passed props that become “global” for a subtree:

  • Theming (currentTheme).
  • Locale (currentLanguage).
  • Dependency injection (store).

The advantage over props is that you don’t need to pass them manually through every component. The advantage over importing is that you can override it from outside the component, which is handy for testing, server rendering, and for things that change.

The downside of context is that it has severe issues with updates, so don’t rely on its value updating correctly. For example, React Redux can safely pass store down because store itself never changes and has its own subscription mechanism.

In general we don’t suggest using context in your application code directly. It’s fine if some libraries use it because they will be easier to migrate when its API changes.


Finally, I’m not a natural painter, but here’s a doodle to sum it up:

doodle

like image 167
Dan Abramov Avatar answered Oct 22 '22 16:10

Dan Abramov