Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active March 8, 2018 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanflorence/4eff368134f7f47705a4c853f0fe8a6c to your computer and use it in GitHub Desktop.
Save ryanflorence/4eff368134f7f47705a4c853f0fe8a6c to your computer and use it in GitHub Desktop.
// 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)}/>
)
}
}
@robertleib
Copy link

Do you have any examples of testing render callback components like the Fetch component above?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment