简体   繁体   English

无法在反应导航中使用setParams将函数设置为参数

[英]Unable to set a function as param using setParams in react-navigation

I'm trying to set scroll to top function in a screen which contains ListView when already focused tab is clicked. 我试图在单击包含焦点的选项卡时在包含ListView的屏幕中设置滚动到顶部功能。

I'm using react-navigation 2.0.0 and react-native 0.59.8 Here is how my navigator looks like: 我正在使用react-navigation 2.0.0和react-native 0.59.8这是我的导航器的样子:

FeedScreen.js returns
   render() {
    return <Nodes {...this.props} />;
  }

Nodes.js
   callScrollToTop = () => {
     this.listView.scrollToOffset({ offset: 0, animated: true });
   }
   componentDidMount(){
     this.props.navigation.setParams({
        scrollToTop: this.callScrollToTop
     })
   }
   // Here, I am able to set a string param like name: "Alice" and that can be seen in navigation state on TabBarOnPress 

const Feed = createStackNavigator({
  Feed: {
    screen: FeedScreen
  }
});

const Profile = createStackNavigator({
  Profile: {
    screen: ProfileScreen
  }
});

 const TabsScreens = createBottomTabNavigator(
  {
    Feed,
    Profile
  },
  {
    navigationOptions: ({ navigation }) => ({
    tabBarOnPress: ({ navigation }) => {
        if (
          navigation.isFocused() &&
          navigation.state.params &&
          navigation.state.params.scrollToTop
        ) {
          navigation.state.params.scrollToTop();
        }
   })
  })

I don't want an exact solution like scrolling list view to top. 我不想要像将列表视图滚动到顶部这样的确切解决方案。 Ability to set a function as param on componentDidMount and access it on tabBarOnPress is enough. 能够在componentDidMount上将函数设置为param并在tabBarOnPress上访问它就足够了。 Please help me find a solution to this. 请帮助我找到解决方案。

Thanks!! 谢谢!!

In your respective tab component use these, 在您各自的标签组件中使用这些,

import { NavigationEvents } from 'react-navigation';

render() {
   return (
    <View>
    <NavigationEvents
       onWillFocus={(route) => {
         if (route.action.routeName === /* check the screen name mentioned in the routes */) {
           //call the function u need
         }
       }}
     />
    // add other components as usual
    </View>
   );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 react-native和react-navigation无法将参数作为参数传递给NavigationOptions中的下一个屏幕 - react-native and react-navigation Unable to pass a function as a param to the next screen in NavigationOptions 如何在 react-navigation navigationOptions 上设置功能? - How set function on react-navigation navigationOptions? 无法解析 React-Navigation - Unable to resolve React-Navigation 无论如何,不​​调用setParams(反应导航)就可以重新渲染导航栏吗? - Is there anyway to re-render navation bar without calling setParams (react-navigation)? 无法使用npm安装react-navigation软件包 - Unable to install react-navigation package using npm 使用 @react-navigation/stack 中的 createStackNavigator,无法渲染堆栈屏幕 - Using createStackNavigator from @react-navigation/stack, unable to render a stackscreen 使用setState与setParams来反应Native - React Native using setState with setParams 屏幕切换时的反应导航调用功能 - React-navigation call function on screen change 无法通过 react-navigation 的导航道具黑白屏幕 - unable to pass navigation props b/w screens with react-navigation 如何使用无状态组件中的react-navigation将getParam参数传递到另一个屏幕以触发graphql突变? - How would I pass a getParam param to another screen using react-navigation in a stateless component to fire up a graphql mutation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM