A javascript library for building user interfaces
React is small, not a complete solutions
UI are everywhere
React is declarative
Declarative for dynamic data
Many smart decisions
Focus in application level
Disadvantages about framework
Philosophy
- Write programs that do one thing and do it well.
- Write programas to work together.
- Write programas to handle text streams, bacause that is a universal interface.
- A language to model the state of UIs, not the transactions on them
Components
Reactive update
Virtual views in memory
Function Component
const MyComponent = (props) => {
return (<domElementOrComponent ... />)
}
props dont change
Class COmponent
Class
MyComponent
extends React.Component {
render() {
return (<dome ../>)
}
}
states changes
JSX is Not HTML
Install React DevTools Extension
JSX to JS (babel)
< div> Hello React < /div> React.createElement("div",null,'Hello React!')
React has some rules uppercase in first letter
< Button /> !== < button />
React has a special function useState()
return [
state object getter (getter),
updater function (setter)
]
useState called hook
Is because each one of these elements get translated to function call
Use array in dynamic way
[< Button />,< Display />]
Use the children of another React element
< div>
< Button />
< Display />
< /div>
Use a special object enclose multiple children
< React.Fragment >{...children} < React.fragment />
or
<> {...children}</>
One way flow of data
Parents components can flow their data down to children components. Parents component can also flow down behavior to children
Responsibility
Important decision, where to define state?
Down in a tree as close as possible to the children who need to acces that value on the state.