diff --git a/CheatSheets/React-Cheatsheet.md b/CheatSheets/React-Cheatsheet.md new file mode 100644 index 0000000..9ac2378 --- /dev/null +++ b/CheatSheets/React-Cheatsheet.md @@ -0,0 +1,698 @@ +# πŸ“š React Cheat Sheet +This repository is a cheat sheet to React for daily use. It contain a lot of snippets from my own use / official documentation and i'll improve it soon ! +It's made for people like me who like to continue have a overview of some snippets. + +## Table of Contents + +1. **[React](#1---react)** +2. **[Redux](#2---redux)** + +## 1 - React +### Use React +```javascript +// Import React and ReactDOM +import React from 'react' +import ReactDOM from 'react-dom' +``` + +```javascript +// Render JSX into a DOM element +ReactDOM.render( +

Hello, world!

, + document.getElementById('root') +); +``` + +```javascript +// Render Component into a DOM element +ReactDOM.render( + , + document.getElementById('root') +); +``` +**[⬆ Go to top](#table-of-contents)** + +### JSX +```javascript +// JSX produce React Element +const item =

My JSX Element

; +``` + +```javascript +// Use curly braces to embed some Javascript +const item =
{getContent()}
; +``` + +```javascript +// Use camelCase for attribute name +const item =
; +``` + +```javascript +// Use curly braces to embed some Javascript +const item = ; +``` + +```javascript +// Self close if tag is empty +const item =
; +``` +**[⬆ Go to top](#table-of-contents)** + +### Components +```javascript +// Stateless Functional Component +function Heading(props) { + return

{props.title}

; +} +``` + +```javascript +// Stateless Functional Component (with arrow function) +const Heading = (props) => { + return

{props.title}

; +} +``` + +```javascript +// ES6 Class Component, can have states +class Heading extends React.Component { + render() { + return

{this.props.title}

; + } +} +``` + +```javascript +// Always start component names with capital + +``` +**[⬆ Go to top](#table-of-contents)** + +### Render +```javascript +// Return React Element +render() { + return
Example of return
+} +``` + +```javascript +// Return Another Component +render() { + return +} +``` + +```javascript +// Return String +render() { + return 'Return a string works!' +} +``` + +```javascript +// Return Numbers (rendered as text node) +render() { + return 100 +} +``` + +```javascript +// Return nothing +render() { + return null +} +``` +**[⬆ Go to top](#table-of-contents)** + +### Component Lifecycle +```javascript +componentWillMount() { + +} +``` + +```javascript +componentDidMount() { + // Call after the component output has been rendered in the DOM +} +``` + +```javascript +componentWillReceiveProps() { + +} +``` + +```javascript +shouldComponentUpdate() { + +} +``` + +```javascript +componentWillUpdate() { + +} +``` + +```javascript +componentDidUpdate() { + +} +``` + +```javascript +componentWillUnmount() { + +} +``` + +```javascript +componentDidCatch() { + +} +``` +**[⬆ Go to top](#table-of-contents)** + +### Props (Properties) +Props are immutable. + +```javascript +// Component with a single valid argument : props +function Heading(props) { + return

{props.title}

; +} +``` +**[⬆ Go to top](#table-of-contents)** + +### State +State are locals and fully controlled by the component itself. + +```javascript +// Update state with setState, except in constructor +this.setState({ + title: 'Updated title', +}); +``` + +```javascript +// Set state with previous state +this.setState((prevState, props) => { + return {count: prevState.count + 1}; +}); +``` + +```javascript +// Declare initial state in constructor +class Heading extends React.Component { + constructor(props) { + super(props); + this.state = {title: 'My title'}; + } +} +``` + +```javascript +// Do not update state directly +this.state.title = 'Hello'; +``` + +```javascript +// Lifting state up to share state between component +class Wrapper extends React.Component { + constructor(props) { + super(props); + this.handleInputChange = this.handleInputChange.bind(this); + this.state = {value: ''}; + } + + handleInputChange(value) { + this.setState({value}); + } + render() { + const value = this.state.value; + return ( + + ); + } +} + +class Input extends React.Component { + constructor(props) { + super(props); + this.handleChange = this.handleChange.bind(this); + } + + handleChange(e) { + this.props.onInputChange(e.target.value); + } + + render() { + const value = this.props.value; + return ; + } +} +``` +**[⬆ Go to top](#table-of-contents)** + +### Handling Event +```javascript +// React Event are in camelCase + +``` + +```javascript +// Use preventDefault instead of return false +function handleClick(e) { + e.preventDefault(); +} + +``` + +```javascript +// Bind this to use it in the callback +constructor(props) { + super(props); + this.handleClick = this.handleClick.bind(this); +} +``` + +```javascript +// Pass data to callback + + +``` +**[⬆ Go to top](#table-of-contents)** + +### Conditional Rendering +```javascript +// Using if operator with props +function Heading(props) { + const isHome = props.isHome; + if (isHome) { + return ; + } + return ; +} +``` + +```javascript +// Using if operator with state +render() { + const isHome = this.state.isHome; + let heading = null; + if (isHome) { + heading = ; + } else { + heading = ; + } + + return ( +
+ {heading} +
+ ); +} +``` + +```javascript +// Using ternary operator +
+ {isHome ? : } +
+``` + +```javascript +// Using logical operator +
+ {messages.length > 0 && +

+ You have messages +

+ } +
+``` + +```javascript +// Prevent component from rendering +function Modal(props) { + if (!props.isShow) { + return null; + } + + return ( +
+ Modal +
+ ); +} +``` + +**[⬆ Go to top](#table-of-contents)** + +### Portal +```javascript +import { createPortal } from "react-dom"; + +class MyPortalComponent extends React.Component { + render() { + return createPortal( + this.props.children, + document.getElementById("node"), + ); + } +} +``` +**[⬆ Go to top](#table-of-contents)** + +### Fragment +```javascript +const Fragment = React.Fragment; + +render() { + return ( + + Some text. +

A heading

+ Even more text. +
+ ); +} +``` + +```javascript +render() { + return ( + + Some text. +

A heading

+ Even more text. + + ); +} +``` + +```javascript +render() { + return ( + <> + + + + ); +} +``` +**[⬆ Go to top](#table-of-contents)** + +### Forms + +#### Controlled Components +```javascript +// In controlled component, each state mutation have an handler function +class Form extends React.Component { + constructor(props) { + super(props); + this.state = {value: ''}; + + this.handleChange = this.handleChange.bind(this); + this.handleSubmit = this.handleSubmit.bind(this); + } + + handleChange(e) { + this.setState({value: e.target.value}); + } + + handleSubmit(e) { + alert('Submitted value: ' + this.state.value); + e.preventDefault(); + } + + render() { + return ( +
+ + +
+ ); + } +} +``` + +```javascript +// Force to uppercase in handler +handleChange(e) { + this.setState({value: e.target.value.toUpperCase()}); +} +``` + +```javascript +//