Total: {this.state.count}
);
}
// 3. update state
}
```
]
---
class: content
## Components can manage their own state
.medium.dim-1.dim-2.dim-3.dim-4.dim-5.dim-14[
```javascript
class Counter extends React.Component {
// 1. initialize state
// 2. render state
// 3. update state
increment() {
this.setState((prevState) => {
return {
count: prevState.count + 1
}
});
}
}
```
]
---
class: content
## React is Component-Based
# Components work best as pure functions
???
In other words, given the **same inputs**, you'd get the **same outputs**.
They make your code **easier to follow**.
When a component is managing state, it's not pure
This doesn't mean you shouldn't manage state
It means you should **isolate the state management**
So that you can write **more pure functional components**
---
class: title
# What isn't React?
---
class: content
## What Isn't React?
# Application-level state management
???
Typically handled by another framework
---
class: content
## State Management
# Flux, Redux, Mobx, etc
???
And I don't want to get too much into this conversation
Because it is a talk of its own
But there is one important thing about the state management options for React
---
class: content
## State Management
# Unidirectional flow
???
I don't have a definition
But you can guess how ridiculous it would be
---
class: content
## Unidirectional flow
.unidirectional-flow[

]
???
Blue = React. Green = state mgmt
This is decidedly different from an approach like Angular,
where there is two-way binding.
...
Why do we care?
---
class: content
## Unidirectional flow
# Leads to a more predictable system
???
The **simplicity** and **predictability** of one-way data flow makes it much easier to figure out what's going on.
---
class: title
# Why would I use React?
---
class: content
## Why React?
# It is fast!
???
And you can say that for most modern javascript frameworks
What makes React fast?
---
class: content
## React is fast!
# Updating the DOM is slow
???
It is often the bottleneck in your UI
React updates the DOM efficiently
---
class: content
## React is fast!
# Virtual DOM
???
by using a Virtual DOM
A lightweight representation of the actual browser's DOM.
When a React component receives new props,
React re-renders it to the virtual DOM
---
class: content
## React is fast!
# Reconciliation (Virtual DOM diffing)
???
And then React uses a process called Virtual DOM diffing
To **identify the differences** between the DOM and the virtual DOM
And it is able to update only the DOM elements **that have changed.**
This process makes React fast!
---
class: content
## Why React?
# Compile-time checking & props validation
.full-width[

]
???
When I think about how many hours of my life I've spent thanks to a property in my view
being named differently than a property in my controller...
It makes me pretty happy.
---
class: content
## Why React?
# It's easy to understand
---
class: content
## It's easy to understand
> Components work best as pure functions
.empty[]
> State is managed with unidirectional flow
???
It's easy to look at a component and understand what it is doing.
There are many little things about React
that are like **little nuggets of functional programming**
---
class: content
## Why React?
# It's easy to test
???
Like previous point -
I can write a **test** that passes specific props into my component
and **reliably** see what happens.
There are also some really great **testing tools** that
make testing in React not only easy, but enjoyable.
---
class: content
## Why React?
# It's easy to collaborate
???
because you're **breaking up** your UI into **tiny components**
**devs and ux designers**/"front-end" developers
can easily **work around each other**
it is similar to how "design by contract" allows you to define how pieces will work together
then individuals can run off and build the pieces.
---
class: content
## Why React?
# It's mostly just JavaScript
---
class: content, double-stack
## It's mostly just JavaScript
```html