Skip to content

Commit

Permalink
Allow POP and similar actions to refresh the previous scene (#1016)
Browse files Browse the repository at this point in the history
* pop actions can pass props to previous screen

* fix typo

* fix eslint warnings

* Document pop with refresh in readme

* Do refreshTopChild if no refresh param is provided

* Fix eslint warns
  • Loading branch information
doomsower authored and aksonov committed Aug 8, 2016
1 parent 41d3213 commit 3ec175f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ import {Actions} from 'react-native-router-flux'
And then:

* `Actions.ACTION_NAME(PARAMS)` will call the appropriate action and params will be passed to the scene.
* `Actions.pop()` will pop the current screen. It can also take a param `{popNum: [number]}` that allows to pop multiple screens at once.
* `Actions.pop()` will pop the current screen. It accepts following optional params:
* `{popNum: [number]}` allows to pop multiple screens at once
* `{refresh: {...propsToSetOnPreviousScene}}` allows to refresh the props of the scene that it pops back to
* `Actions.refresh(PARAMS)` will update the properties of the current screen.

## Production Apps using react-native-router-flux
Expand Down
14 changes: 11 additions & 3 deletions src/Reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ function resetHistoryStack(child) {
);
}

function refreshTopChild(children, refresh) {
if (refresh) {
const topChild = children[children.length - 1];
return [...children.slice(0, -1), { ...topChild, ...refresh }];
}
return children;
}

function inject(state, action, props, scenes) {
const condition = ActionMap[action.type] === ActionConst.REFRESH ? state.key === props.key ||
state.sceneKey === action.key : state.sceneKey === props.parent;
Expand Down Expand Up @@ -75,7 +83,7 @@ function inject(state, action, props, scenes) {
return {
...state,
index: targetIndex,
children: state.children.slice(0, (targetIndex + 1)),
children: refreshTopChild(state.children.slice(0, (targetIndex + 1)), action.refresh),
};
}

Expand Down Expand Up @@ -104,7 +112,7 @@ function inject(state, action, props, scenes) {
...state,
index: state.index - popNum,
from: state.children[state.children.length - popNum],
children: state.children.slice(0, -1 * popNum),
children: refreshTopChild(state.children.slice(0, -1 * popNum), action.refresh),
};
}
case ActionConst.REFRESH:
Expand All @@ -126,7 +134,7 @@ function inject(state, action, props, scenes) {
...state,
index: ind,
from: state.children[state.index],
children: state.children.slice(0, ind + 1),
children: refreshTopChild(state.children.slice(0, ind + 1), action.refresh),
};
}
return {
Expand Down

1 comment on commit 3ec175f

@ismeteor
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi borther, how to use Actions.pop(refresh,{})

Please sign in to comment.