Notes On React
Why React ?
React is a javascript library for creating UIs.
La gran diferencia entre libreria y framework es que el fw nos aisla del lenguaje, y react nos obliga a utilizarlo.
Web Paradigm
htmlDOM <-> JS <-> CSS -> render tree-> layout -> paint
Web Applications
El que gestiona las interfases es el lenguaje de programacion
Android -> XML. Java procesa ese xml y lo convierte en interfases visibles para el usuario
Principles of reactive functional programming.
Abstraction/ Encapsulation -> Components
Data Flow -> state
UI/ UX -> Frontend
-
Componentes
identificar los componentes ? Que pregunta deberíamos hacernos para identificar los componentes ?
Si tienen comportamiento y sentido en si mismo: por ejemplo Corazoncito de twitter, que se pina de rosa cuándo le doy click. Dos estados y se repit en la interfase muchas veces.const e = React.createElement;
class LikeButton extends React.Component {
constructor(props) {
super(props);
this.state = { liked: false };
}
render() {
if (this.state.liked) {
return ‘You liked this.’;
}
return e(
'button',
{ onClick: () => this.setState({ liked: true }) },
'Like'
);
}
}
const domContainer = document.querySelector(‘#like-button-container’);
ReactDOM.render(e(LikeButton), domContainer);
Comportamiento y Objetivo. Es un principio si tiene sentido en si mismo.
Se repite en la interfaz
2. Estados
3. Flujos
v dom poder calcular diferencias
Cuándo cambia un dato en ReactDOM, sólo esa parte se vulve a dibujar
Cuándo cambia un dato en Javascript TODO el DOM se vuelve a dibujar.
React
ReactDOM
JSX: Es una respuesta a poder integrar de frma muchoa más simple html y javascript. La sintaxis es más agradable a la vista.
Para poder compilar JSX usamos Babel. Compatibilidad de JSX y ES5 y ES6.
var element = <h1>Esto es un elemento</h1>
Babel lo compila cómo
React.createElement("h1", null, "Esto es un elemento")
#### Understanding Basic Reactivity Concepts
As the web was originally designed to build Hypertext documents and not for creating Apps, first came Javascript to interact allow us to interact with the DOM in the browser. But Javascript in the browser is not reactive. So, what would be a reactive DOM ? in this example, we want to build an App that stores the name written in an input in a variable that replicates the change on every keypress event. So the initial name value in our app would be `name = ''` and every time we press a key we'd like to have our name's *state* mutated as well. if you have a value stored in a variable, and you want it to be synced.
```javascript
// $ touch example1.html
// Create an html file and add paste this code in a