如何在React Native中使用Navigator对正在转换为的组件调用方法
How to call method on component that is being transitioned to with Navigator in React Native?
我的主要导航组件如下所示:
class Main extends React.Component {
constructor() {
super();
this.state = {
views: [
{ title: 'Page 1', component: React.createElement(PageOne) },
{ title: 'Page 2', component: React.createElement(PageTwo) },
{ title: 'Page 3', component: React.createElement(PageThree) }
],
currentView: 'Page 1'
};
}
changeScene(data) {
if (this.refs.navigator.getCurrentRoutes().find(r => r.title === data.title))
this.refs.navigator.jumpTo(data);
else
this.refs.navigator.push(data);
this.setState({currentView: data.title});
}
renderScene(route, navigator) {
return route.component;
}
render() {
return (
<View>
<Navigator
ref="navigator"
style={{flex: 1}}
initialRoute={this.state.views[0]}
renderScene={this.renderScene} />
</View>
);
}
};
在导航器中将Page组件转换为之后,我想调用它上的方法(类似于onTransitionedTo
)。我尝试了以下方法:
changeScene(data) {
if (this.refs.navigator.getCurrentRoutes().find(r => r.title === data.title))
this.refs.navigator.jumpTo(data);
else
this.refs.navigator.push(data);
this.setState({currentView: data.title});
if (data.component.onTransitionedTo)
data.component.onTransitionedTo();
}
但这显然是行不通的。实现这一目标的最佳方式是什么?
也许可以尝试在componentWillMount或其他适当的生命周期方法上执行传递的回调,这样组件就可以检查它是否已转换为,而不是父级直接在子级上执行某些操作。
主要:
React.createElement(PageOne, { isActive: () => this._isActive(PageOne) })
...
_isActive(component) {
let routes = this.refs.navigator.getCurrentRoutes();
return component === routes[routes.length - 1];
}
组件内:
componentWillMount() {
if (this.props.isActive()) {
// Set state to affect render
}
}
相关文章:
- 将自定义Google Sheets功能转换为附加组件
- 如何处理 React / Flux 组件中的状态转换
- 如何将自定义组件与反应路由器路由转换一起使用
- 将 D3 块转换为 Enyo 组件
- 如何将字符串转换为URI组件
- 如何在React Native中使用Navigator对正在转换为的组件调用方法
- 淘汰赛3.2-'如果'基于AMD加载组件之间的转换
- 当组件是片段视图时,添加道具和转换
- 使用JQuery插件来转换React组件中的DOM
- 将单个React组件转换成JS小部件
- 将旧代码转换为余烬组件
- 我如何让Babel与transform-react-jsx正确转换组件没有模块.导出(供ACE编辑器使用)
- 是否可以将Immutable.js Map转换为react组件中的Object ?
- 将有状态的React组件转换为无状态的功能组件:如何实现componentdidmount一种功能性
- 如何任意转换组件渲染输出
- 类型转换:组件到RadioButtonStyle
- 通过路由转换维护组件状态- EmberJS
- 不同敲除组件之间的转换
- 如何在 vue.js 的组件中加载数据时触发转换
- 将MS Office文件转换为PDF,使用edraw office viewer组件OCX和javascript