React - Js Cheatsheet
React - Js Cheatsheet
js cheatsheet
DEVHINTS.IO Edit
React is a JavaScript library for building user interfaces. This guide targets React v15 to v16.
import React from 'react' <Video fullscreen={true} autoplay={fals class Info extends Component {
import ReactDOM from 'react-dom' render () {
const { avatar, username } = this.p
import React, {
Use the React.js jsfiddle to start hacking. (or
Component,
the unofficial jsbin) States Fragment
} from 'react'
constructor(props) {
super(props) class Info extends Component {
this.state = { username: undefined } render () {
Import multiple exports
} const { avatar, username } = this.p
See: States
https://devhints.io/react 1/9
4/24/24, 2:44 AM React.js cheatsheet
Children
<AlertBox>
<h1>You have pending notifications</h
</AlertBox>
Defaults
Other components
https://devhints.io/react 2/9
4/24/24, 2:44 AM React.js cheatsheet
this.state
this.props
Functional components have no state. Also, Performance-optimized version of
their props are passed as the first parameter React.Component. Doesn’t rerender if
These methods and properties are available
to a function. props/state hasn’t changed.
for Component instances.
See: Function and Class Components See: Pure components
See: Component API
Lifecycle
Mounting Updating
constructor (props) Before rendering # componentDidUpdate (prevProps, prevState, snapshot) Use setState()
here, but
componentWillMount() Don’t use this # remember to
compare props
render() Render #
shouldComponentUpdate (newProps, newState) Skips render()
componentDidMount() After rendering (DOM available) #
if returns false
Hooks (New)
https://devhints.io/react 3/9
4/24/24, 2:44 AM React.js cheatsheet
import React, { useState } from 'react'; import React, { useState, useEffect } from 'react';
If you’re familiar with React class lifecycle methods, you can think of
useEffect Hook as componentDidMount, componentDidUpdate, and
Declaring multiple state variables
componentWillUnmount combined.
import React, { useState } from 'react'; By default, React runs the effects after every render — including the first
render.
function ExampleWithManyStates() {
// Declare multiple state variables!
const [age, setAge] = useState(42);
const [fruit, setFruit] = useState('banana');
const [todos, setTodos] = useState([{ text: 'Learn Hooks' }
// ...
}
https://devhints.io/react 4/9
4/24/24, 2:44 AM React.js cheatsheet
Define FriendStatus
Also see: Hooks FAQ
import React, { useState, useEffect } from 'react';
Basic Hooks
function FriendStatus(props) {
useState(initialState)
const [isOnline, setIsOnline] = useState(null);
useEffect(() => { … })
useEffect(() => {
function handleStatusChange(status) {
useContext(MyContext) value returned from React.createContext
setIsOnline(status.isOnline);
}
Full details: Basic Hooks
ChatAPI.subscribeToFriendStatus(props.friend.id, handleSt
return () => { Additional Hooks
ChatAPI.unsubscribeFromFriendStatus(props.friend.id, ha
}; useReducer(reducer, initialArg, init)
}, [props.friend.id]);
useCallback(() => { … })
if (isOnline === null) {
return 'Loading...'; useMemo(() => { … })
}
return isOnline ? 'Online' : 'Offline'; useRef(initialValue)
}
useImperativeHandle(ref, () => { … })
DOM nodes
https://devhints.io/react 5/9
4/24/24, 2:44 AM React.js cheatsheet
Other features
See Transferring props There are more, but these are most common.
JSX patterns
Inner HTML
Always supply a key property.
<Fragment>
{showMyComponent
? <MyComponent />
: <OtherComponent />}
</Fragment>
Short-circuit evaluation
<Fragment>
{showPopup && <Popup />}
...
</Fragment>
New features
https://devhints.io/react 6/9
4/24/24, 2:44 AM React.js cheatsheet
See: Hydrate
Property validation
https://devhints.io/react 7/9
4/24/24, 2:44 AM React.js cheatsheet
string
MyCo.propTypes = {
number
Required types user: PropTypes.shape({
name: PropTypes.string,
func Function age: PropTypes.number
MyCo.propTypes = {
})
bool True or false name: PropTypes.string.isRequired
}
}
Enum
Use .array[Of], .object[Of],
oneOf(any) Enum types
.instanceOf, .shape.
Required
(···).isRequired Required
Also see
https://devhints.io/react 8/9
4/24/24, 2:44 AM React.js cheatsheet
Devhints home
Flux architecture React-router Vim scripting Vue.js
cheatsheet cheatsheet cheatsheet cheatsheet
https://devhints.io/react 9/9