Josh David Miller PRO
startup advisor, speaker, CEO. More than mildly obsessed in seeing founders succeed. I only use my powers for good.
product strategist, speaker, advisor, entrepreneur
@joshdmiller
joshdavidmiller.com
<Navbar />
<Album />
<Player />
<App>
</App>
<Cover />
<Header />
<TrackList />
<Album>
</Album>
<Header />
<Track />
...
<TrackList>
</TrackList>
<Navbar />
<Album>
<Player />
<App>
</App>
</Album>
<Cover />
<Header />
<Header />
<Track />
...
<TrackList>
</TrackList>
...
<Navbar />
<Album>
<Player />
<App>
</App>
</Album>
<Cover />
<Header />
<Header />
<Track />
...
<TrackList>
</TrackList>
...
We're trying to play a song from the Track component.
We're trying to mutate state from the Track component.
or
<Navbar />
<Album>
<Player />
<App>
</App>
</Album>
<Cover />
<Header />
<Header />
<Track />
...
<TrackList>
</TrackList>
...
onClick
onTrackClick
onTrackClick
create a new state
...
( state ) => ui
∴
( state ) => ui
<Navbar />
<Album>
<Player />
<App>
</App>
</Album>
<Cover />
<Header />
<Header />
<Track />
...
<TrackList>
</TrackList>
...
React is a JavaScript library for rendering UI based on state.
models
controllers
observers
scopes
templates
2-way data binding
dependency injection
ui = f ( state )
const Header = React.createClass({
render () {
return (
<div>
<h1>Hello, world!</h1>
</div>
);
},
});
ReactDOM.render( <Header />, document.getElementById( 'app' ) );const Track = ({
id,
idx,
title,
artist,
duration,
onClick,
}) => (
<div className='track' onClick={e => onClick( id )}>
<span>{ idx }</span>
<span>{ title }</span>
<span>{ artist }</span>
<span>{ duration }</span>
</div>
);const Track = ( state ) => ui;const Track = ( state ) => (
<div className='track' onClick={e => onClick( id )}>
<span>{ idx }</span>
<span>{ title }</span>
<span>{ artist }</span>
<span>{ duration }</span>
</div>
);const Track = ({
id,
idx,
title,
artist,
duration,
onClick,
}) => ui;const Track = ({
id,
idx,
title,
artist,
duration,
onClick,
}) => (
<div className='track' onClick={e => onClick( id )}>
<span>{ idx }</span>
<span>{ title }</span>
<span>{ artist }</span>
<span>{ duration }</span>
</div>
);<html>
<head>
<title>Reacstasy Example</title>
</head>
<body>
<div id="app" />
</body>
</html>import React from 'react';
import ReactDOM from 'react-dom';
import Track from 'components/track';
function log ( id ) {
console.log( `You clicked on track ${id}.` );
}
ReactDOM.render(
<Track
id='123'
idx='0'
title='Hello, Dolly'
artist='Louis Armstrong'
onClick={log}
/>, document.getElementById( 'app' )
);
Flux is a UI architecture with a uni-directional data flow.
asks for data, listens for changes
updates if necessary, tells listeners data changed
gets action requests, dispatches to stores
a description of an event
providing data to
dispatching
connected through
Redux is a predictable state container for JavaScript apps.
{
}
currentAlbum: { ... },
playlist: [ ... ],
currentTrack: { ... },
user: {
email: 'josh@joshdmillercom',
preferences: { ... },
},
( state ) =>
({
});
repeat: state.user.preferences.repeat,
track: state.currentTrack,
{
}
type: 'PLAY_TRACK',
track: 1234,
( state, action ) => {
};
switch ( action.type ) {
}
case 'PLAY_TRACK':
return {
...state,
currentTrack: action.track,
};
default:
return state;
case 'SKIP_TRACK':
return {
...state,
currentTrack: /* ... */,
};
// ...
function dispatch ( action ) {
this.state = functions.reduce( function ( state, fn ) {
return fn( state, action );
}, this.state );
this.listeners.forEach( fn => fn() );
}asks for the data it needs
description of what happened
immutable POJO
reduce a new state
Everything is a Component
( state ) => ui
State is Immutable
Data Flows Down
Events Bubble Up
( state, action ) => state
By Josh David Miller
An overview of data flow with React and Redux. Part of the introductory lesson to a React/Redux Live class: https://www.codementor.io/classes/learn-reactjs-and-redux-live
startup advisor, speaker, CEO. More than mildly obsessed in seeing founders succeed. I only use my powers for good.