Skip to content

Commit 752dde2

Browse files
author
Pavlo Aksonov
committed
add refresh action to change props for current route without transitions
1 parent 5b1225c commit 752dde2

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

Actions.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const BEFORE_DISMISS = 'BEFORE_ROUTER_DISMISS';
1010
const AFTER_DISMISS = 'AFTER_ROUTER_DISMISS';
1111
const AFTER_FOCUS = 'AFTER_ROUTER_FOCUS';
1212
const BEFORE_FOCUS = 'BEFORE_ROUTER_FOCUS';
13+
const AFTER_REFRESH = 'AFTER_ROUTER_REFRESH';
14+
const BEFORE_REFRESH = 'BEFORE_ROUTER_REFRESH';
1315

1416
function isNumeric(n){
1517
return !isNaN(parseFloat(n)) && isFinite(n);
@@ -104,6 +106,18 @@ class Actions {
104106
}
105107
return res;
106108
}
109+
refresh(props: { [key: string]: any} = {}){
110+
props = filterParam(props);
111+
let router: BaseRouter = this.currentRouter;
112+
if (router.delegate.props && router.delegate.props.dispatch){
113+
router.delegate.props.dispatch({...props, type: BEFORE_REFRESH, route:router.currentRoute, name:router.currentRoute.name})
114+
}
115+
const res = router.refresh(props);
116+
if (router.delegate.props && router.delegate.props.dispatch){
117+
router.delegate.props.dispatch({...props, type: AFTER_REFRESH, route:router.currentRoute, name:router.currentRoute.name})
118+
}
119+
return res;
120+
}
107121
pop(num: number = 1, props: { [key: string]: any} = {}){
108122
props = filterParam(props);
109123
if (!isNumeric(num)){

BaseRouter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ export default class BaseRouter {
211211
return this.delegate.onDismiss && this.delegate.onDismiss();
212212
}
213213

214+
refresh(props:{ [key: string]: any} = {}) {
215+
return this.delegate.onRefresh && this.delegate.onRefresh(props);
216+
}
217+
214218

215219
}
216220

ExRouter.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,6 @@ export class ExRouteAdapter {
161161
}
162162
}
163163

164-
class ExNavigationBar extends Navigator.NavigationBar {
165-
constructor(props){
166-
super(props);
167-
this.state = {};
168-
}
169-
render(){
170-
const route = this.props.router.nextRoute || this.props.router.currentRoute;
171-
if (route.props.hideNavBar === false){
172-
return super.render();
173-
}
174-
if (this.props.router.props.hideNavBar || route.props.hideNavBar){
175-
return null;
176-
}
177-
return super.render();
178-
}
179-
}
180-
181164
export default class ExRouter extends React.Component {
182165
router: BaseRouter;
183166

@@ -280,10 +263,29 @@ export default class ExRouter extends React.Component {
280263
this.setState({modal: null});
281264
}
282265

266+
onRefresh(props:{ [key: string]: any}){
267+
this.setState(props);
268+
}
269+
283270
onActionSheet(route: Route, props:{ [key: string]: any}){
284271
this.refs.actionsheet.showActionSheetWithOptions({...route.props, ...props}, props.callback);
285272
}
286273

274+
_renderNavigationBar(props){
275+
const navBar = this.props.renderNavigationBar ? this.props.renderNavigationBar(props) :
276+
<Navigator.NavigationBar {...props}/>
277+
278+
const route = this.props.router.nextRoute || this.props.router.currentRoute;
279+
if (route.props.hideNavBar === false){
280+
return navBar;
281+
}
282+
if (this.props.router.props.hideNavBar || route.props.hideNavBar){
283+
return null;
284+
}
285+
return navBar;
286+
287+
}
288+
287289
render() {
288290
const router = this.props.router;
289291
if (!router){
@@ -301,13 +303,13 @@ export default class ExRouter extends React.Component {
301303
{header}
302304
<ExNavigator ref="nav" initialRouteStack={router.stack.map(route => {
303305
const oldProps = router.routes[route].props
304-
router.routes[route].props = {...oldProps, ...parentProps(this.props)}
306+
router.routes[route].props = {...oldProps, ...parentProps(this.props), ...this.state}
305307
return new ExRouteAdapter(router.routes[route])
306308
})}
307309
style={styles.transparent}
308310
sceneStyle={{ paddingTop: 0, backgroundColor:'transparent' }}
309-
renderNavigationBar={props=><ExNavigationBar {...props} router={router}/>}
310311
{...this.props}
312+
renderNavigationBar={props=>this._renderNavigationBar({...props, ...this.state, router})}
311313
/>
312314
{footer}
313315
{this.state.modal}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-router-flux",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"description": "React Native Router using Flux architecture",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)