Skip to content

Commit

Permalink
feat: Pass event.params to RENDER/NAVIGATE by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelkaron committed Oct 17, 2019
1 parent 17e1799 commit 4394083
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ export class XStateRouterTest {
initial: 'overview',
states: {
overview: {},
details: {
entry: assign({
params: (ctx, event) => event.params || ctx.params
})
}
details: {}
},
meta: {
component: 'is-test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import {
EventObject,
interpret,
Interpreter,
OmniEventObject,
StateMachine,
TransitionDefinition,
StateNode,
OmniEventObject
TransitionDefinition
} from 'xstate';
import {
ComponentRenderer,
NavigationHandler,
Route,
RouteHandler,
RouterEvent,
RouterInterpreterOptions,
StateRenderer
} from './types';
Expand Down Expand Up @@ -138,22 +139,27 @@ export class XstateRouter implements ComponentInterface {
this.service = service
// add transition handler that triggers RENDER on state transition
.onTransition(state => {
const {
changed,
meta,
context: { params: stateParams },
event
} = state;
const { params: eventParams } = event as RouterEvent;
// return fast if state has not changed and is not the initial state
if (!state.changed && state !== initialState) {
if (!changed && state !== initialState) {
return;
}
// default merge to true if not passed in options
const { merge = true } = this.options || {};
// optionally merge state.meta before descruction
const { component, params, slot } = merge
? mergeMeta(state.meta)
: state.meta;
const { component, params, slot } = merge ? mergeMeta(meta) : meta;
// if there's a component we RENDER
if (component) {
send('RENDER', {
component,
slot,
params: { ...params, ...state.context.params }
params: { ...params, ...stateParams, ...eventParams }
});
}
// get url by reducing state path and matching route
Expand All @@ -164,7 +170,7 @@ export class XstateRouter implements ComponentInterface {
if (path) {
send('NAVIGATE', {
path,
params: { ...params, ...state.context.params }
params: { ...params, ...stateParams, ...eventParams }
});
}
})
Expand Down

0 comments on commit 4394083

Please sign in to comment.