Last active
April 8, 2019 10:46
-
-
Save faceyspacey/7142893f50f032bb9c25e025f7e34beb to your computer and use it in GitHub Desktop.
Respond Modular Components
This file contains hidden or 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
export default createApp({ | |
components: { | |
App: (props, { location }) => { | |
const Component = location.components.list.ComponentWithHoistedDataDeps | |
return Component ? <Component /> : <Spinner /> | |
}, | |
}, | |
routes: { | |
LIST: { | |
path: '/list/:category', | |
load: async (request, action) => { | |
const module = await import('./component-with-hoisted-data-deps') | |
return module.default({ | |
callbacks: { | |
beforeEnter: 'beforeEnter', | |
thunk: 'thunk', | |
onEnter: 'onEnter' | |
}, | |
deps: { | |
category: action.params.category, // map params to deps!! | |
} | |
}) | |
} | |
} | |
} | |
}) | |
// component-with-hoisted-data-dep.js: | |
export default ({ | |
deps, | |
callbacks: { | |
beforeEnter = 'beforeEnter', | |
thunk = 'thunk', | |
onEnter = 'oneEnter' | |
} | |
}) => createModule({ | |
routes: { | |
entry: { | |
[beforeEnter]: [verifyAccess, saveScroll], | |
[thunk]: ({ api }) => api.get(`items/${deps.category}`), | |
[onEnter]: restoreScroll, | |
} | |
}, | |
components: { | |
ComponentWithHoistedDataDeps: (props, state) => ( | |
<ul> | |
{state.items.map(item => <li>{item.title}</li><)} | |
</ul> | |
) | |
}, | |
reducers: { | |
items: (state, actions) => action.payload | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lastly, for this to work, the implementation must allow for multiple such components included in a single route, and their callbacks are merged into the array form. For example, we already support:
route.thunk: [callback1, callback2]
They don't currently in parallel. They are sequential (and can be async). So we need to implement a special case where when module components are combined into the same route, they are run in parallel.