Skip to content

Instantly share code, notes, and snippets.

@atticoos
Created July 24, 2020 17:53
Show Gist options
  • Save atticoos/aa3307aad60c03dee068797153079046 to your computer and use it in GitHub Desktop.
Save atticoos/aa3307aad60c03dee068797153079046 to your computer and use it in GitHub Desktop.
function MyComponent ({spaceId}) {
const [space, loading, error] = useSpace(spaceId);
if (loading) return <Loading />
if (error) return <Error />
return <SpaceCard space={space} />
}
function useSpace (spaceId) {
const dispatch = useDispatch();
const {space, spaceLoading, spaceError} = useSelector((state) => ({
space: state.entities.spaces[spaceId],
spaceLoading: state.entities.meta.space[spaceId].isLoading,
spaceError: state.entities.meta.space[spaceId].error
}));
useEffect(() => {
if (!space) {
dispatch(getSpace(spaceId))
}
}, [spaceId]);
return [space, spaceLoading, spaceError]
}
const selector = (state, props) => ({
space: state.entities.spaces[props.spaceId],
spaceLoading: state.entities.meta.space[props.spaceId].isLoading,
spaceError: state.entities.meta.space[props.spaceId].error
})
class MyComponent extends React.Component {
compeontDidMounts() {
this.props.dispatch(getSpace(this.props.spaceId))
}
render() {
if (this.props.spaceLoading) return <Loading />
if (this.props.spaceError) return <Failure />
return <SpaceCard space={this.props.space} />
}
}
export default connect(selector)(MyComponent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment