【问题标题】:state params undefined using react-navigation使用反应导航未定义的状态参数
【发布时间】:2019-11-28 22:38:40
【问题描述】:

我可以导航到屏幕,但参数未定义,状态只是:

{
key: "id-1574950261181-7",
routeName: "Video Player"
}

导航自:

  render() {
    const {
      id,
      title,
      description,
      video,
      preview,
      push,
      dispatch,
      testLink,
      navigate
    } = this.props;

    return (
      <TouchableHighlight
        style={styles.episode}
        activeOpacity={1}
        underlayColor="#808080"
        onPress={() => {
          navigate("VideoPlayer", { tester: id });
        }}
      >
        <View style={styles.title}>
          <Text style={styles.text}>{title}</Text>
        </View>
      </TouchableHighlight>
    );
  }

我的VideoPlayerScreen(为简洁起见)给了我:

import React from "react";
import {
...
} from "react-native";
...
...
class VideoPlayerScreen extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
    this.onShare = this.onShare.bind(this);
    this.navigateScreen = this.navigateScreen.bind(this);
    this.bookmarkVideo = this.bookmarkVideo.bind(this);
    this.loadRecapVideo = this.loadRecapVideo.bind(this);
  }
  ....
  render() {
    const {
       videos,
       bookmarkVideo,
       navigate,
       state: {
         params: { id }
       }
     } = this.props;
    console.log(this.props.navigation.state.params);
    let videoProps = videos.find(obj => obj.id == id);

    return (<View />)
  }
}

const mapDispatchToProps = dispatch => ({
  bookmarkVideo: id => dispatch(bookmarkVideo(id))
});

const mapStateToProps = state => {
  return {
    videos: state.tcApp.videos
  };
};

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(VideoPlayerScreen);

【问题讨论】:

  • 我仍然无法理解const {navigate}=this.props 是如何工作的。应该是const {navigation} = this.propsconst {navigate} = this.props.navigation

标签: react-native react-navigation


【解决方案1】:

如果你使用react-native-navigation 4x,你应该改变props变量

来自:

const { ... navigate, } = this.props; 到:

const { ... navigation, } = this.props;

然后这样称呼它:

onPress={() => { navigation.navigate("VideoPlayer", { tester: id }); }}

官方文档:React Native Navigation Docs

友好。

【讨论】:

  • 感谢我使用 ^3.0.9
猜你喜欢
  • 2019-09-26
  • 1970-01-01
  • 2018-11-10
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 2017-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多