A Naming Convention for Injector Components

Kyle Poole
2 min readMay 22, 2017

--

What is an “injector component”?

An injector component takes props, optionally computes new ones, then injects them into its child via React.cloneElement(). Crucially, it also does not add any new components to the DOM. It only exists in React's virtual DOM.

When Are Injector Components Useful?

In the prototyping framework, constelation, we use a LOT of injector components to abstract out logic and nicely separate concerns like style, animation, and interactions from our layout components. React Native has also used this pattern in TouchableWithoutFeedback.

Here is an example render using some injector components:

But here’s the thing, do you know which of these components are injectors? How many elements are actually added to the DOM?

Introducing the <Injector_ > Naming Convention

With constelation, we've chosen to denote injector components with a postfix _. Think of the postfix _ as being a blank space that needs to be filled by a child. Also, the name chains well. Below could read like: "an Evented, Animated, Styled View with a Text child"

Notice how much easier it is to identify the injectors? Also, we can now easily see that there are two elements added to the DOM (two do not have a postfix _).

It Gets Better

Can you spot the errors in this render?

Yes! Injector components should never be self-closing, and they should never wrap multiple children. We can fix this at code-time and not wait for the errors at runtime.

This naming convention helps developers understand the component’s contract, and certainly would’ve helped me better understand how TouchableWithoutFeedback and TouchableOpacity differ.

Even Better with Tooling

Naming conventions enable tooling. I’ve edited my vim color scheme to color injector components as props are colored, which further reinforces the concept and allows me to easily skim renders and identify them.

In the future, we could also create lint rules to identify errors at code-time using this convention.

more naming conventions => more helpful tooling

Other naming conventions

Originally published in the react-playbook

--

--