From 7c8a1539f574e660d6dbe269b06a6221f3dddcfc Mon Sep 17 00:00:00 2001 From: Marcello Lamonaca Date: Mon, 15 Mar 2021 18:04:54 +0100 Subject: [PATCH] Start React notes --- JavaScript/React/React.md | 222 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 JavaScript/React/React.md diff --git a/JavaScript/React/React.md b/JavaScript/React/React.md new file mode 100644 index 0000000..ffda712 --- /dev/null +++ b/JavaScript/React/React.md @@ -0,0 +1,222 @@ +# React + +## Components + +Thera are two types of react componets: + +- Function Components +- Class Components + +Both types can be stateful and have side effects or be purely presentational. + +```jsx +// functional component +const Component = (props) => { + return ( + + ); +} + +// class component +class Component extends React.Component { + return ( + + ); +} +``` + +*NOTE*: a component name *must* start with an uppercase letter. + +Every components has two inputs: *props* and *state*. The props input is explicit while the state is implicit. State is used to determine the chages. Within the component state can be changed while the props object represent fixed values. + +JSX syntax can resable HTML bat gets converted to pure JavaScript before being sent to the browser: + +```js +// JSX +const element = ( +

Hello, world!

+); + +// compiled JS shipped to browser +const element = React.createElement( + 'h1', // HTML tag name + {className: 'greeting'}, // attrs as JSON + 'Hello, world!' // tag content (can be nested component) +); +``` + +### App Entrypoint + +```js +ReactDOM.render( + , + // same as + React.createElement(RootComponent, null); + + document.getElementById("RootNode_parentId") // add to DOM +); +``` + +### Dynamic Expressions + +```js +{expression} // expression is evalueted an it's result is displayed +{expression} + func(args)}>{expression} +``` + +### Props + +```js + // pass a value the component + // pass a function to the component + +function Component(props) { + // use props with {props.propName} +} + +class Component extends React.Component{ + // use props with {this.props.propName} + render() +} +``` + +### Simple Function Component + +```js +// Button.js +import { useState } from "react"; + +function Button() { + const [count, setCount] = useState(0); // hook + + const handleCLick = () => setCount(count + 1); // logic + + // JSX + return ( + + ); +} + +export default Button; +``` + +### Simple Class Component + +```js +class Button extends React.Component { + + state = {count: 0}; + //or + constructor(props) { + super(props); + this.state = {count: 0}; + } + + handleClick = () => { + this.setState({ count: this.state.count + 1 }); + } + // or + handleClick = () => { + this.setState((state, props) => ({ count: state.count + props.increment }) ); + } + + render(){ + return ( + + ); + } +} +``` + +### Nesting Compnents + +```js +import { useState } from "react"; + +function Button(props) { + return ( + + ); +} + +function Display (props) { + return ( +
{props.message}
+ ); +} + +function App() { + + // state must be declare in the outer component it can be passed to each children + const [count, setCount] = useState(0); + const incrementCounter = () => setCount(count + 1); + + return ( +
+
+ ); +} + +export default App; +``` + +### User Input (Forms) + +```js +function Form() { + const [userName, setUserName] = useState(""); + + handleSubmit = (event) => { + event.preventDefault(); + // ... + } + + return( +
+ setUserName(event.target.value)} // needed to update UI on dom change + required + /> + +
+ ); +} +``` + +### Lists of Components + +```js +// ... +
+ {array.map(item => )} +
+// ... +``` + +**NOTE**: The `key` attribute of the component is needed to identify a particular item. It's most useful if the list has to be sorted. + +## Hooks + +### `useState` + +Hook used to create a state object. + +`useState()` results: + +- state object (getter) +- updater function (setter) + +```js +const [state, setState] = useState(default); +```