smoosh
“Rockstar developer”
Rockstar
Everything but a rockstar
“He rocked the stage”
He read his notes from a made-on-a-plane
slides trembling for 30 minutes while
praying that people won't out he’s a fraud
Rolling Stones in front of 1.5m people
 At the end of the day we're all code
monkeys trying to make weird libraries
work together by writing shitty hacks
🙈 🙉 🙊
I am Kitze $👋
(A Rockstar Senior Ninja Developer)
I am Kitze $ 👋
💖 Open Source
💖 Making products
💖 Frontend
React, React Native, Redux, MobX, GraphQL,
Apollo, CSS, css-in-js, etc.
reactacademy.io
const loginToMac = ({user, password}) => {
return user === 'root' && password === '';
}
Login in High Sierra
State management
in a GraphQL era
TL;DR
GraphQL is the thing that’s eventually
gonna replace REST, but you keep
telling yourself that you don’t need to
learn it.
REST
/api/currentUser/activity
/api/currentUser/friends
/api/user/5/activity
/api/user/5/likedPosts
GraphQL
query {
currentUser {
activity {
posts {
title
dateCreated
}
}
friends {
name
avatarUrl
activity {
likedPosts {
id
imageUrl
}
}
}
}
}
SPAs ruined everything
🤦
SPAs ruined everything
State management
Data
Forms
Routing
Tabs
NavigationMenus
Filters
Date pickers
Checkboxes
Inputs
Data is the #1 reason why
state management is hard
Data fetching
Caching
Reading
Cache invalidating
jQuery
JustAngular™
React
/api/posts
/api/posts/1
/api/user/info
Trying to be too smart
Redux
I love Redux
I hate Redux
“Hello Reddit 👋 I just started my first React
app.
What should I use to do network requests?”
“Just use redux-saga”
Explaining Redux to someone
Right now, there is a poor soul
trying to connect redux-saga with redux-form
just to make a frickin’ login form
Explaining Redux to someone
MobX
jQuery vs MooTools
Angular vs Ember
GraphQL vs Rest
Redux vs MobX
React vs Vue
Apollo vs Relay
Hillary vs Trump
Mistake: Asking
❓what’s better❓
instead of
✅ what’s suitable for my app
✅ what’s suitable for my team
✅ what’s suitable for our use-case
Trump is great…
on a golf course.
HIGHER ORDER COMPONENTS SUCK !!!111 🤬
RENDER PROPZZ ARE EVERYYTHINNGG <3 <3
HOW ABOUT A HEALTHY MIX? 🍏
WE NEED REDUX FOR EVERYTHING !!!!1111
REDUX IS DEAD ☠
WE CAN JUST REPLACE IT WITH THE NEW
CONTEXT RIGHHTTT GUISE??!111 🙄
HOW ABOUT A HEALTHY MIX? 🍏
Examples
fetch('api/todos').then(function(response) {
return response.json();
}).then(function(todos) {
console.log(todos);
});
Fetch
const fetchTodos = () => dispatch => {
dispatch({type: 'FETCH_TODOS_INIT'});
api.fetchTodos().then(todos => {
dispatch({type: 'FETCH_TODOS_SUCCESS', todos});
}).catch(error => {
dispatch({type: 'FETCH_TODOS_FAIL', error})
})
};
Redux - async action
const todosReducer = (
state = {loading: false, todos: [], error: null},
action
) => {
switch (action.type) {
case 'FETCH_TODOS_INIT': {
return { ...state, loading: true};
}
case 'FETCH_TODOS_SUCCESS': {
return { ...state, loading: false, todos: action.todos};
}
case 'FETCH_TODOS_FAIL': {
return { ...state, loading: false, error: action.error};
}
}
};
Redux - reducer
class App {
@observable todos = [];
@observable loading = false;
@observable error = null;
@action fetchTodos = () => {
this.loading = true;
api.fetchTodos().then(todos => {
this.todos = todos;
this.loading = false;
}).catch(error => {
this.error = error;
this.loading = false;
});
}
}
MobX
GraphQL
@graphql(gql`
query {
todos {
title
date
}
}
`)
class Todos extends Component {
render(){
const {data: {loading, todos}} = this.props;
{loading ? <div> loading ... </div> : <ul>
{todos.map(todo => <li>{todo.title} - {todo.date} </li>)}
</ul>}
}
}
GraphQL
We were fixing the sink
instead of the well
the sink = state management
the well = REST apis
State management
Data
Forms
Routing
Tabs
NavigationMenus
Filters
Date pickers
Checkboxes
Inputs
State management
Forms
Routing
Tabs
NavigationMenus
Filters
Date pickers
Checkboxes
Inputs
Do we even need a state
management library when using
GraphQL?
1. Vanilla
2. I hate forms
3. My users live in tunnels
4. I wanna go home early
5. Next level
6. Apollo
- Apollo
- Router library
🍦Vanilla
- setState()
- Apollo
- Router library
😡 I hate forms
- Form library
- setState()
- Apollo
- Router library
🚅 My users live in tunnels
- apollo-cache-persist
- Apollo
- MobX
🏡 I wanna go home early
- mobx-router
- mobx-react-form
- Apollo
- Next.js
🔺 Next level
- setState()
🔺 Next level 2.0
Use full page reloads when
navigating between routes
Treat every route as a separate mini-app
😱
/
/about
/products
/product/123 /cart
App 1
App 2
App 4
App3
App5
Only Apollo?
apollo-link-state
Client side schema
@graphql(gql`
query {
todos {
title
completed
date
}
counter @client
}
`)
class Todos extends Component {
render() {
}
}
When using GraphQL, in 90% of the
cases, you won’t need Redux, MobX,
sagas, observables, RxJS or *insert
hipster state-management library here*
But who am I to tell you what to do?!
I work alone
I don’t work for clients
I experiment with a new library every week
I don’t know what I’m doing
I’ll make a Christmas tree out of this slide
Evaluate the context,
and your situation, before you follow
other people’s advice
A client project story
Don’t be scared of the network!
Few kilobytes of data
are better than your team spending
weeks on overengineering things.
I used to overengineer things in my past.
Now I’m just trying to make my users
happy 💜
🖐
✅ I made something
✅ It works and I think it’s awesome
😞 I’m not sure if it’s right
What is “right”?
Router example(s)
Nomadlist example(s)
A hospital website
🤦 🤦 🤦 🤦 🤦 🤦 🤦 🤦 🤦 🤦 🤦 🤦
Don’t feel bad for not using the
latest and greatest
Service worker example
🖐
Stop seeking external approval 🙏
Stop seeking answers in other people’s projects 👪
Stop feeling insecure about your code because
nobody actually knows what they’re doing 😕
For God’s sake delete your Twitter account 🙅
If you like what you’re doing and it works
for you, please forget about this talk
and move on.
✅ kitze.io
😞 @thekitze on Twitter
✅ @kitze on GitHub
✅ @kitze on Medium
Thank you 🙏
@thekitze
reactacademy.io
@kitze
@kitze

State management in a GraphQL era