【问题标题】:TypeError: navigation.getParam is not a functionTypeError:navigation.getParam 不是函数
【发布时间】:2021-06-15 17:26:00
【问题描述】:

我正在使用带有堆栈 5 版本的反应导航。

App.js

export default class App extends React.Component {
  render() {
     return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen name="Home" component={Home} />
        <Stack.Screen name="Movie" component={Movie} />
      </Stack.Navigator>
    </NavigationContainer>
     )
  }
}

Home.js

     <TouchableHighlight onPress={() => { this.props.navigation.navigate('Movie', { title: item.title, img: item.img, id: item.imdbID }) }}>
      // code omitted               
      </TouchableHighlight>

我单击电影项目以查看电影详细信息,但收到错误消息并显示:

TypeError: navigation.getParam is not a function

Movie.js

   componentDidMount() {
        this.getMovieDetails(this.props.navigation.getParam('id', 'n/a')) 
    }

    getMovieDetails = async (id) => {
         const results = await fetchById(id)
         this.setState({ info: results })
    }

    render() {
        const { navigation } = this.props
        const title = navigation.getParam('title', 'N/A');
        const img = navigation.getParam('img', 'https://banner2.kisspng.com/20180216/kee/kisspng-photographic-film-reel-clip-art-movie-film-5a8677562304e0.0541516415187618141435.jpg');
}

您能帮我解决这个问题吗? 我正在等待您的回复。

提前致谢

【问题讨论】:

  • 你能在世博会上用这个问题做点心吗?如果是,请与我分享,我会修复它。
  • @WaheedAkhtar snack.expo.io/@adnan.salah84/04697d 注意:不要使用网络。试试安卓或者IOS 谢谢
  • 这是您在此小吃中遇到的问题的解决方案,如果可行,请将我的问题标记为已接受。 snack.expo.io/@waheed25/home_movie

标签: reactjs react-native react-navigation


【解决方案1】:

这是一个包含您需要的解决方案的小吃,我对您的代码进行了一些更改,您的错误现在在这个小吃中得到修复。 You were trying to access getParams function but that wasn't available inside the navigation object 所以而不是getting params with getParams function I directly get these params from this.pros.navigation.route.params

https://snack.expo.io/@waheed25/home_movie

【讨论】:

    【解决方案2】:

    这是react-native 中通过导航传递props 时的常见问题。您可以尝试这种方法,看看它是否有效。

    //Home.js
    
    <TouchableHighlight onPress={() => { this.props.navigation.navigate({'Movie', { title: item.title, img: item.img, id: item.imdbID }}) }}>
    

    Movie.js 中,您可以像这样访问道具。

    //Movie.js
    componentDidMount() {
            this.getMovieDetails(this.props.route.params) 
        }
    

    更新:

     //Home.js
        <TouchableHighlight onPress={() => { this.props.navigation.navigate('Movie', { title: item.title, img: item.img, id: item.imdbID }) }}>
          // code omitted               
          </TouchableHighlight>
    
        //Movie.js
        componentDidMount(){
              const navObject = this.props.navigation.state.params 
              console.log(navObject )
        }
    

    【讨论】:

    • 同样的问题。请在我更新我的第一篇文章时查看我的截图!
    • 我已经更新了答案。你能看看@AdnanSalahuddin
    • 更新 console.log 后。这没什么。我收到有关 navigation.getParam 的错误。它说没有功能。我认为const { navigation } = this.props const title = navigation.getParam('title', 'N/A'); 有问题
    • 你能解决这个问题吗? @AdnanSalahuddin
    • 不。如果你觉得舒服,我会和你分享零食snack.expo.io/@adnan.salah84/04697d
    【解决方案3】:
    this.state = {
        do:{
          name: props.route.params.name,
          color: props.route.params.color
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 2019-12-20
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多