React
Install
Create React project
Basic Scripts
-
Start the development server
This component was made by Stratis Dermanoutsos. The code can be found here. -
Bundle the app into static files for production
This component was made by Stratis Dermanoutsos. The code can be found here. -
Start the test runner
This component was made by Stratis Dermanoutsos. The code can be found here. -
Remove this tool and copy build dependencies, configuration files and scripts into the app directory
This component was made by Stratis Dermanoutsos. The code can be found here.Once you run this, there is no turning back!
JSX
JSX is a syntax extension for JavaScript. It was written to be used by React and looks a lot like HTML. Given JSX is not valid JavaScript, web browsers can’t read it directly.
JSX is an inline markup that looks like HTML and gets transformed to JavaScript. A JSX expression starts with an HTML-like open tag, and ends with the corresponding closing tag. JSX tags support the XML self close syntax so you can optionally leave the closing tag off.
For example,
Components
Components is the heart of React. Their job is only 1: Break UIs into small chunks.
They can be either a function or an ES6 class.
Function components
How to use?
Class components
How to use?
Component Instance
Element
Hooks
UI components are often dynamic.
Developers had to build and use classes to use certain React’s features. While classes are still there, hooks are a more ergonomic option.
Hooks enable the developer to re-use stateful logic. Without them, it is easy, and sometimes even necessary, to wrap components inside components and repeat this process a lot of time, eventually getting lost.
Basically, hooks are functions that generally start with the word ‘use’.
Only call them at the top level of a function!
-
useState()
Its purpose is to handle reactive data. Any data that changes is called state. So, when there is a change in the state, React re-renders the UI to display those changes.
This component was made by Stratis Dermanoutsos. The code can be found here. -
useEffect()
It exists to handle the lifecycle of a component.
The code bellow runs every time a state is changed.
This component was made by Stratis Dermanoutsos. The code can be found here.Example case
This component was made by Stratis Dermanoutsos. The code can be found here. -
useContext()
It allows the developer to work with React’s context API. In other words, share data without passing props.
This component was made by Stratis Dermanoutsos. The code can be found here.In the code above, when the mood value changes inside parent, it changes in the ‘MoodEmoji’ component too.
The useContext() hook is a cleaner version of a ‘Consumer’ component.
-
useRef()
It’s used to grab a value that changes but does not re-render the UI.
This component was made by Stratis Dermanoutsos. The code can be found here.In the code above, the value of ‘count’ changes but the displayed value stays the same.
A more common use is to grab HTML elements from JSX.
This component was made by Stratis Dermanoutsos. The code can be found here. -
useReducer()
It is basically a different way to manage state
This component was made by Stratis Dermanoutsos. The code can be found here. -
useMemo()
It is used to optimize computation cost for improved performance.
It is only recommended for expensive calculations.
This component was made by Stratis Dermanoutsos. The code can be found here. -
useId()
It is a new hook introduced with React 18 and it is used for generating unique IDs on both the client and server.
This component was made by Stratis Dermanoutsos. The code can be found here.
There are more hooks, but I kept it simple and focused to the common ones.
Images
Import styles from CSS
-
The CSS
This component was made by Stratis Dermanoutsos. The code can be found here. -
The Component
This component was made by Stratis Dermanoutsos. The code can be found here.
Make your life easier
Object of React Components
This will serve as a simple library of components. They can be defined inside a single file, let’s say Timeline.js.
Now, to call them, simply do
Short import paths
This is a very common problem when working with React:
import module from '../../models/car';
It’d be much better looking if it was something like:
import module from 'models/car';
To achieve this, simple add the following line inside the compilerOptions in your jsconfig.json or tsconfig.json. (depending on whether you use JavaScript or TypeScript respectively)
'baseUrl': 'src',
It will look something like this:
Suspend Layout
The new <Suspense> tag enables you to render another component while your data is being fetched, without the need of a ternary operator (? :) or anything like that.
Avoid inline function calls
Let’s say you need a button that writes "Hello world!" in the console.
You could do it in an inline style:
or by using a predefined method:
While the inline method can be useful in reducing the lines of code in your file, it greatly reduces readability and makes your program difficult to maintain if used frequently. By defining a method before using it in your JSX, you can write longer functions and still keep them readable.
Deploy to production
Apache
To deploy a React project to apache, there are several steps that must be taken. These steps with ensure that the endpoint will work for all routes and apache won’t throw 404s for not finding a directory.
-
Go to your package.json and paste the following:
"homepage": ".", -
Inside the
<head>tag of your index.html file, paste:<script type="text/javascript"> document.write("<base href='//" + document.location.host + "/' />"); </script>Note that, after the
document.location.host, you may need to paste the name of the directory your application will be deployed in. This is a necessity if your server handles multiple endpoints. To do that:<script type="text/javascript"> document.write("<base href='//" + document.location.host + "/{path}/' />"); </script>and replace the
{path}with the directory’s path. -
Inside the App.js or index.js, depending on where you’ve declared the
<BrowserRouter>, you’ll have to provide the{path}you set in the previous step asbasename. To do that, change<BrowserRouter>into<BrowserRouter basename='{path}'>replacing it like before. -
Inside the project’s public/ directory, create a file called .htaccess and paste:
Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.html [QSA,L] -
Build & deploy
This component was made by Stratis Dermanoutsos. The code can be found here.and paste the generated files inside your Apache directory.
Resources
- Top ReactJS interview questions you need to prepare
- Udemy course by Maximilian Schwarzmüller
- Fireship video (explanation)
- Beginner’s Tutorial by freeCodeCamp.org (2022)
- React Hooks Fundamentals from freeCodeCamp.org
- Web Dev Simplified video (introduction)
- Fireship video (hooks)
- How I structure my react components by Rakesh Potnuru
- Routing in React
- URL parameters and query strings in React
- React with TypeScript Cheatsheet
- React anti-patterns by Fireship
- useful-custom-react-hooks repository by Web Dev Simplified
- Deploy React
- Tailwind CSS with React
- Building a Progressive Web App with React
- Best Component Libraries for React