# ReactJS * [https://reactjs.org/docs/](https://reactjs.org/docs/) * [https://github.com/facebook/react/](https://github.com/facebook/react/) * [https://github.com/facebook/react](https://github.com/facebook/react) * [http://facebook.github.io/react-native/docs/](http://facebook.github.io/react-native/docs/) * [https://github.com/reactjs/redux](https://github.com/reactjs/redux) * [https://github.com/reactjs/react-redux](https://github.com/reactjs/react-redux) * [https://github.com/facebook/react/tree/master/packages/react-dom](https://github.com/facebook/react/tree/master/packages/react-dom) * [https://github.com/ReactTraining/react-router](https://github.com/ReactTraining/react-router) ##### Boilerplates * [https://github.com/mikechabot/react-boilerplate](https://github.com/mikechabot/react-boilerplate) * [https://github.com/kriasoft/react-starter-kit](https://github.com/kriasoft/react-starter-kit) * [https://github.com/react-boilerplate/react-boilerplate](https://github.com/react-boilerplate/react-boilerplate) ``` #### notes #### /// /// // ``` --- * source - [https://github.com/simoneas02/react-cheatsheet](https://github.com/simoneas02/react-cheatsheet) ## 🌈 React Cheat Sheet > A simple cheat sheet for facilitate the process in the workshops and event about React. Let me know if you see any problem, I'll love a pull request for improve this document. ### Table of contents - [x] [Installation](#installation) - [x] [No configuration](#no-configuration) - [x] [ReactDOM](#reactdom) - [x] [Functional Stateless Component](#functional-stateless-component) - [x] [Class Component](#class-component) - [x] [Composition](#composition) - [x] [Module component](#module-component) - [x] [Hot Module Replacement](#hot-module-replacement) - [x] [Props](#props) - [x] [State](#state) - [x] [Methods and Events](#methods-and-events) - [x] [State manipulation](#state-manipulation) - [x] [Bindings](#bindings) - [ ] [Refs](#refs) - [ ] [Keys](#keys) - [ ] [Component Lifecycle](#component-lifecycle) - [ ] [Inline Styles](#inline-styles) - [ ] [React Router](#react-router) - [ ] [Storybook](#storybook) - [ ] [Tests](#tests) - [ ] [a11y](#a11y) - [ ] [API comunication](#api-comunication) - [ ] [Flux](#flux) - [ ] [Redux](#redux) - [ ] [MobX](#mobx) - [ ] [Best Practices](#best-practices) - [ ] [Concepts](#Concepts) - [ ] [Immutable](#immutable) - [ ] [Functionnal programing](#functionnal-programing) - [ ] [Virtual Dom](#virtual-dom) - [x] [ES6](#es6) - [x] [Arrow Functions](#arrow-functions) - [x] [Syntax](#syntax) - [x] [Advanced Syntax](#advanced-syntax) - [x] [Spread Operations](#spread-operations) - [x] [Spread in array literals](#spread-in-array-literals) - [x] [Spread in object literals](#spread-in-object-literals) --- ## Installation * Add the tags in your HTML ``` ``` * Run this scripts in your terminal ``` $ npm install react react-dom ``` **[⬆ back to top](#table-of-contents)** --- ## No configuration Just start with React no configuration (run the scripts bellow in your terminal) * Install the React ``` $ npm install -g create-react-app ``` * Create your application (change `myApp` to your application name) ``` $ create-react-app myApp ``` * Go to the application folder and install the dependencies ``` $ cd myApp $ npm install ``` * Start your application ``` $ npm start ``` * Go to the browser by `URL` bellow and see your beautiful application - [localhost:8080](http://localhost:8080) **[⬆ back to top](#table-of-contents)** --- ## ReactDOM ```js import React, { Component } from 'react'; import ReactDOM from 'react-dom'; ReactDOM.render(

Hello React Ladies

, document.getElementById('root') ); ``` **[⬆ back to top](#table-of-contents)** --- ## Functional Stateless Component ```js import React from 'react'; const Button = () => export default Button; ``` ```js import React from 'react'; const Button = ({ onClick, className = 'button', children }) => export default Button; ``` **[⬆ back to top](#table-of-contents)** --- ## Class Component ```js import React, { Component } from 'react'; class MyComponent extends Component { render() { return (

Helo Devas

); } } export default MyComponent; ``` ```js import React, { Component } from 'react'; class MyComponent () extends Compnent { constructor ( props ) { super(props); this.state = { message: 'Helo Devas' } }; render() { return (

{ this.state.message }

); } } export default MyComponent; ``` **[⬆ back to top](#table-of-contents)** --- ## Composition ```js import React, { Component } from 'react'; import ReactDOM from 'react-dom'; class Love extends Component { render() { return (

My love

); } } class LoveList extends Component { render() { return (
); } } ReactDOM.render( , document.getElementById(´root´) ); **[⬆ back to top](#table-of-contents)** ``` ## Module component ```js //App.js import React, { Component } from 'react'; class App extends Component { render() { return (

My App

); } } export default App ``` ```js //Index.js import React, { Component } from 'react'; import ReactDOM from 'react-dom'; import App from './App.js'; class Index extends Component { render() { return (
); } } ReactDOM.render ( , document.getElementById('root') ); ``` **[⬆ back to top](#table-of-contents)** --- ## Hot Module Replacement * Retain application state which is lost during a full reload. * Save valuable development time by only updating what's changed. * Tweak styling faster -- almost comparable to changing styles in the browser's debugger. ```js import React, { Component } from 'react'; import ReactDOM from 'react-dom'; import MyComponent from './MyComponent'; ReactDOM.render( , document.getElementById('root') ); if (module.hot) { module.hot.accept(); } ``` **[⬆ back to top](#table-of-contents)** --- ## Props ```js import React, { Component } from 'react'; class App extends Component { render() { return (

My App {this.props.name}

); } } class Index extends Component { render() { return (
); } } export default Index; ``` **[⬆ back to top](#table-of-contents)** --- ## State ```js import React, { Component } from 'react'; class App extends Component { constructor(props) { super(props); this.state = {messages: 0}; } render() { return (

My messages: {this.state.messages}

); } } export default App; ``` **[⬆ back to top](#table-of-contents)** --- ## Methods and Events ```js import React, { Component } from 'react'; class App extends Component { escreve() { console.log("Eu te amo"); } render() { return (
); } } export default App; ``` **[⬆ back to top](#table-of-contents)** --- ## State manipulation ```js import React, { Component } from 'react'; class App extends Component { constructor() { super(); this.state = { like: 0 }; } isLiked = () => { this.setState({ like: this.state.like + 1}); } render() { return (
); } } export default App; ``` ```js import React, { Component } from 'react'; class App extends Component { constructor() { super(); this.state = { messages: ['JS', 'React'] }; } addMessages = () => { const updateMessages = [...this.state.messages, 'Polymer'] this.setState({ messages: [...updateMessages] }) } render() { return (
{console.log(this.state.messages) /* ['JS', 'React', 'Polymer'] */}
); } } export default App; ``` **[⬆ back to top](#table-of-contents)** --- d ## Bindings ```js import React, { Component } from 'react'; class MyComponent extends Component { constructor () { super(); this.state = { list: list }; this.doSomethingElse = this.doSomethingElse.bind(this); }; doSomething = () => { // do something /* if don't have a parameter, you can use arrow function and don't need to use bind */ } doSomethingElse ( itemId ) { // do something else } render() { return (
{this.state.list.map( item => ... ... )}
); } } export default MyComponent; ``` --- # ES6 ## Arrow Functions ### Syntax #### Basic syntax ```js ( param1, param2, ..., paramN ) => { statements } ( param1, param2, ..., paramN ) => expression ( singleParam ) => { statements } singleParam => { statements } () => { statements } ``` #### Advanced Syntax ```js params => ({ foo: bar }) /* return an object literal expression */ ( param1, param2, ...ladies ) => { statements } /* rest parameters */ ( language = JS, ladies, ..., framework = React ) => { statements } /* default parameters */ const sum = ( [num1, num2] = [1, 2], { x: num3 } = { x : num1 + num2 } ) => num1 + num2 + num3 /* destructuring within the parameter list */ sum() /* 6 */ ``` --- ## Spread Operations ### Spread in array literals ```js const basics = [ 'JS', 'HTML', 'CSS' ]; const frameworks = [ 'React', 'Vue' ]; const web = [ ...basics, ...frameworks ]; console.log(web); /* ['JS', 'HTML', 'CSS', 'React', 'Vue'] */ const addWeb = [ ...web, 'al11' ]; console.log(addWeb); /* ['JS', 'HTML', 'CSS', 'React', 'Vue', 'al11'] */ ``` ### Spread in object literals ```js const basics = { behavior: 'JS', markup: 'HTML' }; const style = 'CSS'; const web = { ...basics, style }; console.log(web); /* { behavior: "JS", markup: "HTML", style: "CSS" } */ const devFront = { framework: 'react', event: 'React Conf' }; const devBack = { framework: 'django', state: 'cool' }; const cloneDev = { ...devFront }; console.log(cloneDev); /* { framework: 'react', event: 'React Conf' } */ const merged = { ...devFront, ...devBack }; console.log(cloneDev); /* { framework: 'django', event: 'React Conf', state: 'cool' } */ ```