Ultimate React.js Cheat Sheet - Upmostly
Ultimate React.js Cheat Sheet - Upmostly
js Cheat Sheet
All the references you need while building your React
application in a single cheatsheet.
Components
Class Components were a major part of React before introducing React Hooks.
Although most new projects prefer Functional Components, there are still many
React-based projects that are using Class Components.
:
Pure Class Component
function ExampleFunctionalComponent() {
return <h1>Example Class Component</h1>;
}
Functional Components are the future of React. Despite previously being solely used
for stateless UI components, they are now the preferred way of constructing new
components.
:
Pure Functional Component
Pure (Functional) Components always generate the same output given the same
input (state and props).
Imports/Exports
Default Imports
Export default
You can import the default exports from a file using the syntax from above.
Named Imports
Named exports
You can export as many named elements from a file as you wish. To do that you need
to use the `export` keyword without the `default` keyword.
To import the named exports you need to list them explicitly within curly braces
:
with the names they have been exported with.
To create an alias you can use the `as` keyword followed by the new local name,
which will be the name under which the imported component will be used in the
current context.
Import all
You can import all of the named exports from a file by using the `* as syntax`.
Props
The simplest way to use props in Class Components in React. They are read directly
from the props object.
A more efficient way to use props in Class Components is to use the destructuring
assignment. We can then use the props directly.
JS expressions
function App() {
const [year, setYear] = useState(1237);
return (
<div>
<Counter
currentCount={year + 3}
maxCount={year + (year * 200)}
:
maxCount={year + (year * 200)}
/>
</div>
);
}
JS expressions will be evaluated and their result will be passed down as props.
Template Literals
function App() {
const [year, setYear] = useState(1237)
return (
<div>
<Counter
warning={`Current year is ${year}`}
/>
</div>
);
};
Spreading
function App() {
const user = {
name: 'Tommy',
surname: 'Smith'
};
return (
:
return (
<div>
<User name={user.name} surname={user.surname} />
You can spread objects to pass all of their properties as individual props.
With Functional Components, you have to expect props object to be passed from the
parent. You can then access the props on the props object.
Passing props
return (
<TargetComponent
portalName="Upmostly"
userAge={userAge}
isOverEighteen={isOverEighteen}
beigeBackground
/>
);
};
You can pass props to the children/target component by assigning them as attributes
to the JSX elements one by one.
Children props
You can pass the children to components by providing them through JSX. Those need
to be located between the opening and closing tags of the elements you wish to pass
them to.
To use children you can use the children element on the props object. In the above
example, we’re destructuring the object in the component definition and returning the
children in JSX.
Default
defaultProps
:
defaultProps
ExampleComponent.defaultProps = {
name: 'Default name',
surname: 'Default surname',
age: 'Default age',
};
You can define the default props by assigning an object with relevant props to the
defaultProps property available on every component.
Inline
const User = ({
name = 'Default name',
surname = 'Default surname',
age = 'Default age',
color = 'lightgreen'
}) => {
return (
<div style={{ backgroundColor: color, padding: '10px', marginBottom:
<h2>Name: {name}</h2>
<h2>Surname: {surname}</h2>
<h2>Age: {age}</h2>
</div>
);
};
State
:
State in Class Components
Reading state
render() {
return (
<div>
<h2>User account</h2>
<ul>
<li>Username: {this.state.username}</li>
<li>Age: {this.state.userAge}</li>
</ul>
</div>
);
}
}
Modifying state
render() {
return (
<div>
<input
type='text'
value={this.state.username}
onChange={(event) => this.setState({ username: event.target.value
/>
</div>
);
}
}
To update the state you simply have to call the `setState` method accessible via this
keyword. As an argument, you have to pass it an object with the new values of the
properties that should be updated.
Arrays
Objects
You can perform complex operations on the objects stored in state by spreading the
array and overwriting properties with desired changes, whilst keeping the other
changes stored in state as prior.
Reading state
return (
<div>
<h2>User account</h2>
<ul>
<li>Username: {username}</li>
<li>Age: {userAge}</li>
:
<li>Age: {userAge}</li>
</ul>
</div>
);
};
With Functional Components, you can manage state by using the `useState` hook. It
returns two values: the first one is the read-only state variable that can be used to
access the state value, and the second one is the callback that will be used to
update that state variable with the value passed as its argument.
Modifying state
return (
<div>
<input
type='text'
value={username}
onChange={(event) => setUsername(event.target.value)}
/>
</div>
);
};
To update the value of the state you can call the second return value from the useState
hook. You need to pass it the new value of the desired state as an argument in order to
update the current one.
If you have some data that won’t be changing over time then you can simply define it
outside of your component.
Enums
const USER_TYPES = {
ADMIN: 'Admin',
BASIC_USER: 'Basic',
PREMIUM_USER: 'Premium',
VIP_USER: 'VIP',
};
function App() {
const [userType, setUserType] = useState(USER_TYPES.ADMIN)
return (
<div>
<h2>Current user type: {userType}</h2>
</div>
);
}
Enums are a great way to add more structure to your React application. Enums are
very o"en used when a given variable can only take a value that is limited to a subset.
They can help us enforce consistency to avoid runtime errors and increase the
:
code readability.
Hooks
useState
Syntax
return <div></div>;
}
When calling the `useState` hook you need to pass it the default state value (initial
state) for initialization. `useState` will return you a tuple containing the read only
state variable and the setter function to update the value of the read-only state
variable.
Usage Example
In the above example, we’re defining the state variable holding our counter variable.
We’re also using the `setCounter` setter function to handle the incrementation and
decrementation of the value of the counter state variable.
useContext
Creating context
ReactDOM.render(
<<context-name>.Provider value={<context-value>}>
<App />
</<context-name>.Provider>,
document.getElementById('root')
);
To define a new context you need to import `createContext` from React as a named
import. Then you need to save the return value of calling `createContext`. The next
:
step is to wrap the part of the application that you wish to be accessible with the
Context Provider. Finally, you have to pass the value of the context to the value
property.
Using context
...
const <context-variable> = useContext(<context-name>);
console.log(<context-variable>);
To use the context we firstly have to import the `useContext` hook and the context
variable itself. We can access the value of context by passing the as an argument to
the `useContext`. The returned value will be the value of the context itself.
useCallback
Syntax
return <div></div>;
};
The `useCallback` hook takes two arguments: the first one is the callback function
:
and the second one is the dependency array. `useCallback` will trigger the
execution of its callback argument each time any of the elements in the dependency
array changes.
Usage example
...
};
We have written a simple component that computes the user score and returns it.
`memoizedCallback` will be updated when the `userID` changes.
useMemo
Syntax
`useMemo` takes two arguments, the first one is the callback function which computes
the desired value and the second one is the dependency array. It works similarly to the
`useEffect` hook. `useMemo` will re-compute the memoized value each time any of
the elements passed to the dependency array undergoe any changes.
Usage example
...
};
return <div>{userScore}</div>;
};
We have written a simple component which computes the user score and returns it.
`userScore` is computed by the `computeUserScore` function, which is memoized.
The score is re-computed only if there is a change to the `userID` state variable.
useImperativeHandle
:
Syntax
useImperativeHandle(ref, () => ({
<property>: () => {
...
}
}));
The `useImperativeHandle` hook gives us control over the values exposed to the
parent component via the ref handle. From the parent component you can then call
`inputRef.current()`.
useDebugValue
Syntax
...
useDebugValue(<label>);
The `useDebugValue` hook takes a label that will be displayed when debugging
custom hooks.
:
Usage example
return premiumUser;
};
useTransition
Syntax
function App() {
const [<value>, <function>] = useTransition();
...
The `useTransition` hook returns a tuple containing a value that indicates whether
the transition is running, as well as a function to start the transition.
Usage example
:
import { useTransition, useState } from 'react';
function App() {
const [loading, startTransition] = useTransition();
return (
<div>
<h2>{count}</h2>
<button onClick={handleClick}>Increment</button>
{loading && <Loader />}
</div>
);
}
In the above counter example we’re using the loading value to indicate to the users if
there is an update happening. We’re also using the `startTransaction` function to
update the state on every click of the button.
useEffect
Syntax
The `useEffect` hook takes 2 arguments, the first one is a callback function and the
other one is the dependency array. The callback function runs every time React
detects an update to element (ref or any other piece of state) provided in the
dependency array.
Usage Example 1
fetchData()
}, []);
return <div></div>;
};
We’re calling the `fetchData` function that’s responsible for fetching data from the
server. We execute it on every re-render because the dependency array is empty.
Usage Example 2
useEffect(() => {
const fetchData = async (userID) => {
...
};
fetchData(userID)
}, [userID]);
return <div></div>;
}
We’re calling the `fetchData` function that’s responsible for fetching data from the
server. We execute it only when there was a change in the userID state variable.
useReducer
Syntax
return <div></div>;
};
The `useReducer` hook takes two functions as aguments; the first one is the reducer
function used to modify the current state held by the reducer and the second one is
the initial state object. It returns the state variable that can be used to access the state
values and dispatch that triggers state updates.
:
Usage
return (
<>
<h2>Count: {state.count}</h2>
<button onClick={() => dispatch({type: 'decrement'})}>Plus one</button
<button onClick={() => dispatch({type: 'increment'})}>Minus one</button
</>
);
};
In the above example, we have written a counter example. It is initialized with the
reducer function and the initial state object. We’re displaying the current value of the
counter to the users and let them change it by interacting with the two buttons. They
are calling the `dispatch` method with the relevant action object. The reducer
function reads the values of the action object and returns the new piece of state.
useRef
Syntax
:
Syntax
To create a new reference you need to import the `useRef` hook from React. Then
you have to call the hook and save the return value as a variable. Finally you’d have to
pass the to the ref prop on the desired JSX element.
Usage example
return (
<>
<input ref={ref} type="text" />
<button onClick={onClickHandler}>Focus</button>
</>
);
}
In the above example, we have defined a reference and passed it to the input element.
We also created a button element. We’re using the reference to call the focus method
:
on it every time we click on the button.
useLayoutEffect
Syntax
return <div></div>;
};
The `useLayoutEffect` hook takes 2 arguments; the first one is a callback function
and the other one is the dependency array. The callback function runs a"er all the
necessary mutations have been applied to the DOM.
useDeferredValue
Syntax
...
:
};
deferred value.
Usage example
return <div>{deferredValue}</div>;
};
useId
Syntax
...
const id = useId();
A"er importing the `useId` hook from React you can simply assign the value returned
by calling the hook to a variable which will then be assigned a generated unique id
value.
Usage example
:
import { useId } from 'react';
return (
<>
<button id={id}>Hello</button>
</>
);
};
Lifecycle methods
constructor
constructor(props) {
super(props);
this.state = {
portalName: 'Upmostly'
};
this.handleLogin = this.handleLogin.bind(this);
}
A constructor in React is used to initialize state and bind methods inside of the class
component.
:
componentWillUnmount
componentWillUnmount() {
document.removeEventListener("click", this.closeMenu);
}
This is what we call a ‘cleanup‘ method. You can treat it as the opposite of the
`componentDidMount`. It’s the place in which you should cancel any tasks that you
might have initialized when the component was mounting.
`componentWillUnmount̀ is called when the components is removed from the
DOM.
getSnapshotBeforeUpdate
getSnapshotBeforeUpdate() {
return {
tableBackground: this.tableRef.current.offsetHeight >= 85
};
}
This method is called shortly before any of the component output gets appended to
the real DOM. It helps us capture some information that can be useful when calling
`componentDidUpdate`.
static getDerivedStateFromProps
This method is called every time right before the render method.
render
render() {
return <h1>Welcome to {this.state.portalName}</h1>;
}
componentDidMount
componentDidMount() {
axios.get(testEndpoint).then(resp => this.setState({ testData: resp })
}
This method is called when the component is first mounted to the DOM. It is typically
used to fetch data from other applications.
shouldComponentUpdate
shouldComponentUpdate(nextProps) {
if (nextProps.portalName !== this.props.portalName) return true
return false;
}
:
This method is used as something that can help improve the performance of our
components. It’s called before the component gets re-rendered with new props or
state.
componentDidUpdate
componentDidMount(prevProps) {
if (this.props.ID !== prevProps.ID) {
axios.get(testEndpoint)
.then(resp => this.setState({ testData: resp }));
}
}
This method is called right a"er the component re-renders. It’s typically used for DOM
manipulations or for making more efficient requests to other applications.
JSX
Return
return (
<div>Hello from Upmostly!</div>
);
function getGreeting(user) {
if (user) {
return <h1>Hello, {formatName(user)}!</h1>;
}
return <h1>Hello, Stranger.</h1>;
}
lastName: 'Smith',
};
return (
<div>
{getGreeting(user)}
</div>
);
};
JSX can contain JavaScript expressions, such as function calls. Function calls may
return JSX which will be rendered to the screen.
Nesting
return (
<div>
<h1>Hello from Upmostly!</h1>
<button>Click me</button>
:
<button>Click me</button>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
);
React components are meant to return a single top-level JSX element. That element
can have more elements nested within.
In JSX, elements can no longer use the “class” keyword, since that’s restricted to
JavaScript. Instead, we are using the “className” keyword, which does the same thing
as the “class” keyword does in HTML.
Axios
Setup
POST
Syntax
...
userAge: 22,
userName: ‘Mike’
});
You can use the post method available on it to send the POST request to the endpoint
provided as an argument. The second argument will be attached as a payload to the
request.
DELETE
Syntax
...
GET
Syntax
...
You can use the get method available on it to send the GET request to the endpoint
provided as an argument.
...
paramsTwo: paramTwoValue,
paramsThree: paramThreeValue
});
If your request depends on some query params then you can simply pass them as an
object as a second argument to the get method. Axios will handle the rest for you.
:
PUT
Syntax
...
userAge: 22,
userName: ‘Mike’
});
You can use the put method available on it to send the PUT request to the endpoint
provided as an argument. The second argument will be attached as a payload to the
request.
Useful syntax
Conditional operator
Syntax
Ternaries are a shorthand notation for control flow with 2 conditions. If the condition
evaluates to true then the block will run, otherwise the will run.
:
Usage
return (
<div>
<h2>{`User is ${age > 18 ? 'over' : 'under' } 18`}</h2>
</div>
);
}
We’re using the ternary operator to render the correct string. If the user is over 18 then
‘User is over 18’ will be rendered.
Conditional chaining
Syntax
<object-name>?.[<property-name>]
If the exists on then it will be accessed, otherwise the expression will be evaluated to
`undefined`.
Usage
const user = {
personalDetails: {
name: 'Tommy',
surname: 'Smith',
age: 21
:
age: 21
},
subscriptionDetails: {
type: 'premium',
price: 12.99
}
};
user?.subscriptionDetails?.type // premium
user?.subscriptionDetails?.age // undefined
The first expression evaluates to string ‘premium’ while the other one to `undefined`.
Profiler
Syntax
function App() {
return (
<Profiler id=<string-id> onRender={<log-function>}>
...
</Profiler>
);
}
To use the `Profiler` we need to import it from the react package. The next step is
to wrap the portion of the JSX tree that you’d like to have analyzed. You then have to
pass the that’s used to identify the Profiler and the that will process your log.
:
Usage example
function App() {
return (
<div style={{ margin: '50px' }}>
<Profiler id="Counter" onRender={log}>
<Counter />
</Profiler>
</div>
);
}
In this example, we are using the `Profiler` to inspect the data about our custom
Counter component. We’re then logging the data in a form of a table to make it easier
to understand.
&&
return (
<div>
{age > 18 && <MyComponent />}
</div>
);
}
&& operator is o"en used to render components if a given condition is met. If the
condition is not met then it will evaluate to and return `null`.
||
const user = {
personalDetails: {
name: 'Tommy',
surname: 'Smith',
age: 21
},
subscriptionDetails: {
type: 'premium',
price: 12.99
}
};
React 18
ReactDOM
root.render(<App />);
The above code snippet will help you to adapt your code to the React 18 changes.
Fragments
Syntax
Wrap the JSX siblings within the `Fragment` component available on the default
React import from React package.
Short syntax
Styling
Inline
Usage
You can use the inline styling by providing the JSX element with the style prop. It
accepts an object whose keys are camelCased CSS properties corresponding to
values provided as strings.
Usage
CSS modules
Syntax
...
.heading
font-size: 10px;
:
font-size: 10px;
}
You can use the CSS modules by naming your CSS files in the .module.css format. You
can define your styles just like you would in a regular CSS file.
Usage
You can use the CSS modules by importing the styles from the relevant file and
passing the desired style to the `className` prop of the relevant JSX element.
CSS
Usage
import './App.css';
You can use the CSS by importing the relevant CSS file into your component. You can
then add relevant classes by using the `className` property on the relevant JSX
:
then add relevant classes by using the `className` property on the relevant JSX
element.
Styled-components
Syntax
<css-properties>
`;
To use styled components you have to first import `styled` from the `styled-
components` package. You can define the new styled component by using the styled.
“ syntax. You can provide the desired properties between “ just like you would in any
other CSS file.
Usage
};
:
The above code snippet defined a new styled component called `Heading`. It then
uses the styled component just like any other component in the JSX.
Tricks
Rendering arrays
You can render arrays in React efficiently by using the map method. You can map each
of the elements of the array into its corresponding JSX element and render it directly
from the JSX.
Pluralising
Your strings might sometimes depend on a numerical value to display a relevant form
of the noun. You can write a `pluralize` method to handle this for you automatically.
return (
<div>
{
users.length === 0
? <h2>There are no users :(</h2>
: <h2>There are {users.length} users :)</h2>
}
</div>
);
}
If your Application depends on the data stored in the array, you can provide a fallback
in case the array is empty.
Syntax
...
ExampleComponent.propTypes = {
<prop-name>: PropTypes.<type>,
...
};
You have to import `PropTypes` from the `prop-types` package. You can then
define the desired types by writing them in an object notation and assigning them to
the `propTypes` property on your component.
Usage example 1
User.propTypes = {
name: PropTypes.string,
surname: PropTypes.string,
age: PropTypes.number
};
We’re defining name and surname to be of type `string` and age of type
`number`.
Usage example 2
App.propTypes = {
name: PropTypes.shape({
firstName: PropTypes.string,
lastName: PropTypes.string
}),
age: PropTypes.number,
rightHanded: PropTypes.oneOf([true, false]),
};
:
export default App;
You can also manage more complex props shapes using PropTypes.
onClick
Updating state
return (
<div>
{showName && <h2>{userName}</h2>}
<button onClick={() => setShowName(!showName)}>Show user name</button
</div>
);
};
Controlled inputs
return (
<div>
<input value={userInput} onChange={(event) => setUserInput(event.target
</div>
);
};
You can use the `onChange` handler to control the data in your inputs.
onSubmit
Submitting form
You can use the `onSubmit` handler to submit your forms. `onSubmit` is available on
the form tag which has a button on type submit nested.
Controlled inputs
return (
<div>
<input
value={userInput}
onChange={handleInputValueChange}
/>
</div>
);
};
:
};
If the event handler function you have defined only takes an event parameter you can
just pass it to the event handler prop and the event object will be inferred.
ReactDOM API
Strict Mode
Syntax
ReactDOM.render(
<StrictMode>
<App />
</StrictMode>,
document.getElementById('root')
);
Usage: Import `StrictMode` from React and then wrap the part of the application
where you would want to use it.
Your email address will not be published. Required fields are marked *
Email address
Name
Your name
Post Comment
Tutorials Cheatsheets
React.js React.js
Next.js
Svelte
Angular
Vue.js
:
TypeScript
Info
About
Write For Us
Privacy
RSS