van*_*yen 5 navigation tabnavigator react-native ex-navigation react-navigation
我正在编写 React Native 应用程序。我正在使用反应导航来导航屏幕。
我有 2 个堆栈导航器,它们位于选项卡导航器中。当我按下 TabBar 上的选项卡项以刷新其视图时,我想要处理单击事件。
export const HomeStack = StackNavigator({
Home: {screen: ScreenHome},
Detail: {screen: ScreenDetail}
})
export const UserStack = StackNavigator({
User: {screen: ScreenUser}
})
export const Tabbar = TabNavigator({
HomeTab: {
screen: HomeStack,
},
UserTab: {
screen: UserStack,
}
}, {
tabBarComponent: ({ jumpToIndex, ...props}) => (
<TabBarBottom
{...props}
jumpToIndex={(index) => {
if(props.navigation.state.index === index) {
props.navigation.clickButton(); //----> pass props params (code processes)
}
else {
jumpToIndex(index);
}
}
}
/>
),
tabBarPosition: 'bottom'
});
class TabClass extends Component{
constructor(props){
super(props)
this.clickButton = this.clickButton.bind(this)
}
clickButton(){
console.log('click button')
}
render (){
return (
<Tabbar clickButton={()=> this.clickButton()}/>
)
}
}
Run Code Online (Sandbox Code Playgroud)
我想将 clickButton() 函数从 TabClass 组件传递到处理事件单击选项卡栏的代码。当 if(props.navigation.state.index === index) 时,我希望它会调用 clickButton()。我尝试了一下,但没用。有什么办法可以解决我的问题吗?
我尝试了 onNavigationStateChange(prevState, currentState)。
class TabClass extends Component{
constructor(props){
super(props)
this.clickButton = this.clickButton.bind(this)
}
clickButton(){
console.log('click button')
}
_handleNavagationStateChange(prevState, currentState){
console.log('handle navigation state change')
}
render (){
return (
<Tabbar onNavigationStateChange={(prevState, currentState) => {
this._handleNavagationStateChange(prevState, currentState)
}}
clickButton={()=> this.clickButton()}
/>
)
}
}
Run Code Online (Sandbox Code Playgroud)
但是, _handleNavagationStateChange(prevState, currentState) 仅在导航状态更改时运行(例如,如果我停留在 HomeTab,我单击“用户选项卡项”,此函数将运行;如果我停留在 HomeTab,我单击“主页选项卡项”,此函数将运行不跑)。
有什么方法可以处理选项卡项目上的单击事件。
小智 1
自定义TabBar的事件触摸时请尝试以下代码:
import { TabBarBottom, TabNavigator, StackNavigator } from 'react-navigation';
export const MainScreenNavigator = TabNavigator({
Home: { screen: HomeViewContainer },
Search: { screen: SearchViewContainer },
}, {
tabBarComponent: ({ jumpToIndex, ...props, navigation }) => (
<TabBarBottom
{...props}
jumpToIndex = { tabIndex => {
const currentIndex = navigation.state.index;
if (tabIndex == 1) {
// do some thing. Call Native Live Stream Record
} else {
jumpToIndex(tabIndex);
}
console.log('Select tab: ', tabIndex);
}}
/>),
tabBarPosition: 'bottom',
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16591 次 |
| 最近记录: |