Craft By Zen

React Logo
React Logo

📖 4 min read

🔖 learning   programming   Javascript   React

React

This article was written as part of our initial docs. I have many more articles about React, and I’m debating whether I should cover them in a single article, or multiple. Stay tuned.

At Clear Labs, the web app is a front-end application built on top of React. React is a javascript library that, when paired with other libraries, creates a front-end framework. In our project, we have React on the front-end and nginx serving the assets on the back-end.

If you are starting React with no previous knowledge, please start with the official docs.

Once you have familiarized yourself with the library, play around with it on Codesandbox or on your local system using Create React App. If you can build yourself a basic UI, continue reading this wiki.

Base Foundation

To build with React, each developer should hone their vanilla Javascript knowledge. Please refer to the Javascript wiki to see if you have any missing gaps in your knowledge base.

A must for each developer onboarding is a clear understanding of how React works. This includes the following:

What are React’s lifecycle functions? And how are they supplemented with React hooks?

Why would I use a React class component over a functional component? And when?

This post tries to address these questions and many more.

Newer React Functionality (React v15+)

The application uses many techniques that are worth highlighting because we’ve developer our own set of best practices to follow.

Supporting Libraries

Many supporting libraries help support the development of the app. Most of these supporting libraries are open source and have dedicated wiki pages. Here are the highlights:

Components

Our project includes Storybook, an interactive UI tool to develop and document components. In each component, an extra js file is created with the stories suffix. E.g. index.stories.js. This helps with developing components on their own and reduces overhead with creating component properties.

Refactoring class components to functional components

Lifecycle functions can be replaced with useEffect. But be careful, as we mentioned in useEffect vs useLayoutEffect, useEffect is asynchronous and lifecycle functions aren’t a 1:1 match.

componentDidMount() {
// do something
}

// now becomes
useEffect(() => {
// do something
}, []);

Building Components

While the previous section introduced us to components, this section expands on how we write components.

Class or Function?

When creating a new component, start off with a function component. What is a function component?

const FunctionComponent = (props) => <div>Here's the JSX</div>;

A function component is an easier markup to read. To React, a function component vs. a class are indistinguishable. As developers, we aim for clean code. Ask yourself the following questions of whether you might need a class.

Do we need lifecycle functions? If this is yes, evaluate whether you can use Hooks instead. If not, use a class.

Do we need a constructor? Rarely do we need a constructor. If you need one ask what special cases are you doing to state or what the justification is for other constructor needs.

Does the component need private or public methods? On a rare occassion, we may want to expose a public class method. Use a class. Maybe there are private methods a class should have. Use a class.

In general, for most components are function components. With the introduction of hooks, function components can also have state. We have our own section about hooks too.

Component or PureComponent?

If using a class, we can further ask whether a PureComponent should be extended vs. a Component.

Compound Components

Compound components allow you to create and use components which share this state implicitly.

I’ve written a few other React articles, as shown below:


Written by Jeremy Wong and published on .