Last active
March 8, 2018 16:41
-
-
Save ryanflorence/4eff368134f7f47705a4c853f0fe8a6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Declarative | |
const App = () => ( | |
<Fetch url="/users"> | |
{({ data, loading }) => loading ? ( | |
<div>Loading...</div> | |
) : ( | |
<Filter data={data}> | |
{rows=> ( | |
<Graph rows={rows}/> | |
)} | |
</Filter> | |
)} | |
</Fetch> | |
) | |
// A bit imperative, and every time you want to use fetch | |
// you're gonna be doing this loading junk too | |
class App extends React.Component { | |
state = { | |
rows: [], | |
loading [] | |
} | |
componentDidMount() { | |
fetch('/users').then(data => { | |
const rows = filter(data) | |
this.setState({ rows, loading: false }) | |
}) | |
} | |
render() { | |
return this.state.loading ? ( | |
<div>Loading ...</div> | |
) : ( | |
<Graph rows={this.state.rows}/> | |
) | |
} | |
} | |
// or the redux version | |
const fetchUsers = () => ( | |
// doin' that thunk stuff | |
(dispatch) => ( | |
dispatch({ type: LOAD_USERS }) | |
fetch('/users').then(data => { | |
dispatch({ type: USERS_LOADED, data }) | |
}) | |
) | |
) | |
class App extends select(state => ({ | |
rows: state.users.results, | |
loading: state.users.loading | |
}))(React.Component { | |
componentDidMount() { | |
this.props.dispatch(fetchUsers()) | |
} | |
render() { | |
return this.props.loading ? ( | |
<div>Loading ...</div> | |
) : ( | |
<Graph rows={filter(this.props.rows)}/> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have any examples of testing render callback components like the Fetch component above?