https://nodejs.org/en/
JavaScript runtime built on Chrome's V8 JavaScript Engine
> node my-code.js
node package manager
{
"name": "my-awesome-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-scripts": "1.1.5"
}
}
package.json
> npm install
npx create-react-app my-app-name
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<div className="red">foo bar</div>,
document.getElementById('root')
);
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
React.createElment('div', { className: 'red' }, 'foo bar'),
document.getElementById('root')
);
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<div className="red">foo bar</div>,
document.getElementById('root')
);
babel-plugin-transform-react-jsx
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<div className="red">foo bar</div>,
document.getElementById('root')
);
import React from 'react';
import ReactDOM from 'react-dom';
const name = 'Li Hau';
<div className="red">{ name }</div>;
JavaScript expression within {}
import React from 'react';
import ReactDOM from 'react-dom';
const elem = <div className="red">foo bar</div>;
ReactDOM.render(elem, document.getElementById('root'));
JSX is a JavaScript expression itself
import React from 'react';
import ReactDOM from 'react-dom';
const elem = (
<div>
<h1>Hello World</h1>
<p>Shopee</p>
</div>
);
ReactDOM.render(elem, document.getElementById('root'));
JSX tag may contain children
import React from 'react';
import ReactDOM from 'react-dom';
const elem = <input type="input" />;
ReactDOM.render(elem, document.getElementById('root'));
JSX tag can be without children
import React from 'react';
import ReactDOM from 'react-dom';
const elem = <div className="red">foo bar</div>;
ReactDOM.render(elem, document.getElementById('root'));
class → className
for → htmlFor
Â
camelCase
tabindex → tabIndex​
maxlength → maxLength
...
Â
dangerouslySetInnerHTML
https://reactjs.org/docs/dom-elements.html
import ReactDOM from 'react-dom';
ReactDOM.render(element, container, [callback]);
React.createContext(defaultValue)
<Provider value={/* some value */}>
<Consumer>
{value => /* render something based on the context value */}
</Consumer>
props = { amount: 3 }
props = { amount: 10 }
props = { amount: 10 }
props = { amount: 3 }
props = { amount: 10 }
props = { amount: 3 }
props = { amount: 10 }
props = { amount: 3 }
div1.removeChild(button2);
div2.textContent = '10';
<div>
<button>-</button>
<div>10</div>
</div>
div1.removeChild(button2);
div2.textContent = '10';
<div>
<button>-</button>
<div>10</div>
</div>
RECONCILIATION
RENDERING
RECONCILIATION
RENDERING
- "Diffing" algorithm
- Renders
- Rendering
  - React (a.k.a. React DOM)
  - React Native
  - React VR
               - Since React 16
               - Code Name: React Fiber
return new state based on the props
If return null, indicate no state update needed
static method
Called right after component is mounted
Do
DOM nodes
Load remote data through network
Subscription, event listener
componentDidMount and componentWillUnmount works in pair
Called before a mounted component receives new props
return new state based on the props
If return null, indicate no state update needed
static method
Invoked right before the most recently rendered output is committed to eg: the DOM
Any value returned by this lifecycle will be passed as parameter to componentDidUpdate()
Invoked right after updating occurs
Do:
Access updated DOM
Make network request if required
Invoked right before component is unmounted and destroyed
Do:
Cleanup: clear timers, cancel network request, unsubscribe listeners from componentDidMount
Catches error during rendering, lifecycle methods, and in constructor of whole tree under the component
Do:
Log error
Display fallback UI
- Library to manage application state
- Created by Dan Abramov and Adrew Clark
Â