💸React Tips
React CheatSheet
Creating a new React App
Using Create React App
Using a minimal template
Doing CRA from scratch with react-scripts
Bootstrapping the React app
JSX
JSX is really React.createElement
Use curly braces to evaluate a javascript expression
You can't use
if
, orfor
loops inside JSX, they are not expressions
Use map with JSX to render collections
Tip: to avoid many nested parens and curlies, use a variable
Tip: Always remember to include a key property when rendering a collection
IMPORTANT: Your component will likely render without any state the first time
Use an empty array as initial state when using map
Use a guard clause to avoid rendering
useState Hook
Defining a piece of state
the set function only QUEUES an update
the set function can optionally take a callback function
useEffect Hook
Components must be PURE functions
Don't call the
set
function of useState inside a componentuseEffect takes a callback as it's first argument, and a dependency array as it's second argument
The dependency array
Do use useEffect for side effects
When to not use useEffect
Event Handling in React
Events are added to JSX and passed a callback
Don't call the function in the onClick attribute
useContext Hook
Creating a context
Providing data from a component to the context
using the useContext hook
Forms
Make your forms controlled components
Use an onSubmit event on the form tag and remember to prevent default behavior
Using CSS with React
Doing a plain import
Using a CSS Module
Importing Images
Creating a new React App
Using Create React App
Using a minimal template
This uses a minimal template by iansu on github
Doing CRA from scratch with react-scripts
Then edit package.json
and add the following lines:
Bootstrapping the React app
JSX
JSX is really React.createElement
Use curly braces to evaluate a javascript expression
You can't use if
, or for
loops inside JSX, they are not _expressions_
Use map with JSX to render collections
Tip: to avoid many nested parens and curlies, use a variable
Tip: Always remember to include a key property when rendering a collection
IMPORTANT: Your component will likely render without any state the first time
This is the most common problem new React developers run into. When you are fetching remote data, your component will first render with no data. So write your code in your component defensively to avoid this.
Use an empty array as initial state when using map
Use a guard clause to avoid rendering
Sometimes if you have no data, it's better to just render nothing, or a message to the user that the component is "loading".
useState Hook
Defining a piece of state
useState returns an array with two elements, the state itself, and a function to update the state. It accepts the initial value for the state as an argument
the set function only QUEUES an update
If you try to access the state after you've changed it, it won't have changed yet. This is because the set function only queues up a change, it doesn't happen right away.
the set function can optionally take a callback function
Whatever we return from the callback will be the new state. It gets passed the previous state value as the first argument.
useEffect Hook
Components must be PURE functions
They must return JSX or null and not interact with anything outside of the function.
Don't call the set
function of useState inside a component
set
function of useState inside a componentIt should always be called in a useEffect or in an event handler.
useEffect takes a callback as it's first argument, and a dependency array as it's second argument
The dependency array
The dependency array decides WHEN the useEffect callback will run.
undefined
- The callback will run everytime[]
- The callback will run only on the first render.[somevariable]
- The callback will run on the first render and anytimesomevariable
changes.
Do use useEffect for side effects
Common side effects we should put in a useEffect Hook:
Fetch Calls
Reading or writing to localStorage
Reading or writing Cookies
Accessing a global variable (you should avoid this anyway)
When to not use useEffect
When you are just doing rendering logic or calculating a value
When you have an event listener like a click or submit
Event Handling in React
Events are added to JSX and passed a callback
In DOM you might do this:
In React you add them using attributes on the JSX
It's cleaner to define your event callback externally to the JSX
Don't call the function in the onClick attribute
useContext Hook
The use context hook is used along with a context to pass data deeply into a React component tree.
Creating a context
Providing data from a component to the context
using the useContext hook
This lets us access whatever we stored in the context's provider's value attribute
Forms
Make your forms controlled components
This means adding an onChange listener to each form control and storing state locally for each control.
Use an onSubmit event on the form tag and remember to prevent default behavior
Using CSS with React
Doing a plain import
This will just add the css file in a <style>
tag in the <head>
of your HTML.
Using a CSS Module
styles
will be an object full of css class names.
You can then use then on your JSX markup as the className.
React will generate random CSS classnames guaranteed to not conflict with styles from other components.
Importing Images
You can import any image file supported on the web: jpg
, png
, or svg
. Then just use it on the src
attribute of an image tag.
Last updated