Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow POP and similar actions to refresh the previous scene #1016

Merged
merged 6 commits into from
Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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