将 navigation.navigate 传递给子组件

2024-02-04

使用反应导航构建应用程序。我有一个从 firebase 中提取数据并在列表视图中呈现数据的父组件。列表视图渲染组件“ListName”具有 onRowPress 函数,但 this.props.navigation.navigate 未定义,因为导航不处于该子组件的状态。

我尝试了多种将导航属性传递给子级的方法,并从子级内的反应导航导入导航。什么都不起作用。

Thanks!

这是我的 rootStack 导航器:

const RootStackNavigator = StackNavigator(
  {
    Login: { screen: LoginScreen },
    Main: { screen: MainTabNavigator },
    Feedback: { screen: ProvideInstanceScreen }
  },
  {
    navigationOptions: () => ({
      //headerTitleStyle: {
        //fontWeight: 'normal',
        tabBarVisible: false,
        header: null
      //},
    }),
  }
);

这是我的父组件(其状态为 navigation.navigate)

import React from 'react';
import { ListView, StyleSheet, Text, Image } from 'react-native';
import { connect } from 'react-redux';
import _ from 'lodash';
import { MainButton, GenSection } from '../components/common';
import { namesFetch } from '../actions';
import ListName from '../components/ListName.js';

class ProvideScreen extends React.Component {

  static navigationOptions = {
    header: null,
  };


  componentWillMount() {
  //console.log('HOME HOME HOME  PRINT', this.props)
    this.props.namesFetch();

    this.createDataSource(this.props);
  }

  componentDidMount() {
    console.log('PROVIDE PROPS   PRINT', this.props)
    this.createDataSource(this.props);
  }

  componentWillReceiveProps(nextProps) {
    //nextProps are next set of props component is rendered with and this.props is still old props
    console.log('NEXT PROPS', nextProps)
    this.createDataSource(nextProps);
  }


  createDataSource({ names }) {
    var namesArray = names.map(function(a) {return [a.name, a.uid];});
    //console.log('NAMES ARRAY PRINT', namesArray)
    const ds = new ListView.DataSource({
      rowHasChanged: (r1, r2) => r1 !== r2
    });
    this.dataSource = ds.cloneWithRows(namesArray);
  }

  renderRow(user) {
    return <ListName name={user[0]} />;
  }


  render() {
    return (
      <Image
        source={require('../components/images/mainBG.png')}
        style={styles.containerStyle}>
        <Text style={styles.textStyle}>
          Provide Feedback
        </Text>
        <GenSection style={{paddingTop: 8}} />
        <ListView
          enableEmptySections
          dataSource={this.dataSource}
          renderRow={this.renderRow}
          removeClippedSubviews={false}
        />

    </Image>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: 15,
    backgroundColor: '#fff',
  },
  containerStyle: {
    flex: 1,
    width: undefined,
    height: undefined,
    backgroundColor:'transparent'
  },
  textStyle:
  {
    paddingTop: 20,
    paddingLeft: 12,
    fontSize: 32,
    fontWeight: '200',
    color: '#597abc'
  }
});



const mapStateToProps = state => {
  //console.log('STATE PRINT', state.feedbackInput)
  const names = _.map(state.feedbackInput, (val) => {
    return { ...val};
  });
  //console.log('NAMES PRINT', names)
  return { names };
};  //this is lodash   .map iterates over key value pairs and run function which assigns data to groups array



export default connect(mapStateToProps, { namesFetch })(ProvideScreen);

这是我的子组件(状态中没有 navigation.navigate):

import React, { Component } from 'react';
import { Text, TouchableWithoutFeedback, View } from 'react-native';
import { connect } from 'react-redux';
import { ListButton } from './common';

class ListName extends Component {
  onRowPress() {
    console.log('ON ROW PRESS  PRINT', this.props)
    this.props.navigation.navigate('Feedback', this.props);
  }

  componentDidMount() {
    //console.log('LIST NAME LIST NAME  PRINT', this.props)
  }

  render() {
    const name = this.props.name;
    //console.log('LISTNAME PRINT', name)

    return (
      <TouchableWithoutFeedback onPress={this.onRowPress.bind(this)}>
        <View>
          <ListButton>
            <Text style={styles.titleStyle}>
              {name}
            </Text>
          </ListButton>
        </View>
      </TouchableWithoutFeedback>
    );
  }
}

const styles = {
  titleStyle: {
    alignSelf: 'center',
    color: '#fff',
    fontSize: 16,
    fontWeight: '600',
  }
};


const mapStateToProps = state => {
  //console.log('NAMES PRINT', names)
  return state
};  //this is lodash   .map iterates over key value pairs and run function which assigns data to groups array


export default connect(mapStateToProps)(ListName);

基于 React 导航:https://reactnavigation.org/docs/en/connecting-navigation-prop.html https://reactnavigation.org/docs/en/connecting-navigation-prop.html

您只需将 withNavigation 导入到子组件中即可。

import { withNavigation } from 'react-navigation';

然后导出:

export default withNavigation(<YourChildComponent>);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 navigation.navigate 传递给子组件 的相关文章

随机推荐

  • 转换 Symfony2 PHP 实体对象以在 Javascript 中使用

    我正在尝试从 PHP 对象打开数据 如下所示 但我希望能够在 JavaScript 中访问此数据以在图形库中使用 有问题的对象 我需要做的是将其转换为 JSON 编码对象以在 Javascript 中使用 我尝试在 Symfony 中使用
  • 嵌套的 Angular2 异步管道如何解决?

    我对嵌套异步管道如何以及何时在 angular2 模板中解析感到有点困惑 而且文档现在的位置不是很好 所以我希望有人可以提供帮助 我有一个非常简单的 Rxjs Observable 从服务返回 通过Observable of myArray
  • 从已删除的远程分支中获取原点?

    当我做git fetch origin并且 origin 有一个已删除的分支 它似乎没有在我的存储库中更新它 当我做git branch r它仍然显示origin DELETED BRANCH 我怎样才能解决这个问题 您需要执行以下操作 g
  • ASP.NET Core Signalr 无法在 AWS 上运行

    我们有两个应用程序 服务器端 Net Core 2 0 和客户端 AngulerJs 它们托管在AWS elistic容器服务上 另外 还有一层云耀斑 此外 我们正在使用指向我们的 docker 容器的 ALB 我们的解决方案应该有一个实时
  • 在 Chrome 中清除焦点上的 HTML5 占位符属性文本

    有什么办法可以清除吗placeholder焦点上的文字Chrome Firefox 会清除焦点上的文本 但 Chrome 不会 这会让用户感到困惑的是 栏中的文本是键入的 还是占位符文本 即使我将文本颜色更改为浅灰色 我不想为此使用不必要的
  • 从对象字面量获取链接值,onchange--Javascript/HTML select

    我知道如何使用 switch case 例程操作此菜单 但我想将 switch case 更改为对象文字 A 部分知道如何获取 onchange 值并打开一个窗口 B 部分知道如何在对象中的名称 值对中查找值 但前提是给它一个硬编码名称来匹
  • cvs更新错误

    我正在使用 WinCVS 当我尝试更新模块时 我不断收到此错误 cvs 更新中止 从服务器读取 错误 1 这里的实际问题是什么以及如何解决这个问题 我遇到了这个问题和类似的问题 通过尝试 CVSROOT pserver 字符串的变体来解决
  • 二元运算符 + 的错误操作数类型

    我需要一个最多 20 位的数字 并且我正在使用 bigint 它在下面的行给了我这个错误 二元运算符 的操作数类型错误 BigInteger t new BigInteger my number getText toString my nu
  • Spring 没有独特的 bean 类型

    我在 Spring 中遇到了一个服务的两个组件的小问题 我有这个组件 Component public class SmartCardWrapper 和这个 Component public class DummySmartCardWrap
  • 使用 Symfony 和 Doctrine 调用 null 500 上的成员函数 has()

    我在设置要从以下服务文件运行的学说时遇到一些问题
  • 如何使用 root 权限在 VS Code 中调试 Go 文件?

    如何强制 Delve in VS Code 使用 root 权限 我正在尝试调试涉及 gopacket pcap 的 go 文件 hndl err pcapgo NewEthernetHandle ifname err couldn t o
  • iterrows 无法迭代 DataFrame 错误:元组对象没有属性“A”

    当我尝试迭代数据帧时 数据类型以某种方式发生了变化 dates pd date range 20130101 periods 6 df pd DataFrame np random randn 6 4 index dates columns
  • Wtf IE7 - 使用 setTimeout 的 AJAX 调用

    我已经在 Firefox Opera 和 Seamonkey 上对此进行了测试 效果很好 当谈到 Internet Explorer 7 时 它可以工作 但只能达到一定程度 我每隔几秒就会对 PHP 脚本进行一次 AJAX 调用 在 IE7
  • 如何使用 .net 紧凑框架 3.5 隐藏数据网格中的列

    我有一个使用 DataReader 作为其数据源的 DataGrid 我想隐藏数据网格的第一列 我正在使用 net 紧凑框架 3 5 我可以找到 Windows 窗体的示例 但 api 已更改得足够多 以至于它们不起作用 您可以将列样式宽度
  • 根据列值删除 Pandas 中的 DataFrame 行 - 要删除的多个值

    我有一个值列表 在 Python 列表中事先不知道 我的 Panda DataFrame 中的一列不得包含所有行 网络上的所有食谱 例如this one https stackoverflow com questions 18172851
  • 使用 Optimize R 优化向量

    我想使用 R 的优化函数构建自己的优化 目标函数是多样化比率 最大化它 希望它是正确的 div ratio lt function weight vol cov mat dr lt t weight vol sqrt t weight co
  • 当本机库不存在时,如何构建 FFI 箱的 docs.rs 文档?

    我有一个静态链接到库的 sys 箱 货物 toml package links foo 1 0 构建 rs fn main println cargo rustc link lib dylib foo 1 0 当我发布包时 docs rs无
  • 如何使用水平投影清理二值图像?

    我想使用二元过滤器从车牌中删除除文本之外的任何内容 我在每个轴上都有投影 但我不知道如何应用它 我的想法是擦除白色轮廓 这是我现在正在工作的图像 这是 X 轴上的投影 from matplotlib import pyplot as plt
  • 在 Firebase 中执行多位置更新时如何使用事务?

    在我的 Firebase 数据库中 我需要同时对两个位置进行两次写入 我对这两个位置都有规则 以确保用户在不同时写入另一个位置的情况下无法在那里写入 对这些位置之一的写入需要递增 递减 当然 这必须通过事务来完成 否则我无法保证用户不会覆盖
  • 将 navigation.navigate 传递给子组件

    使用反应导航构建应用程序 我有一个从 firebase 中提取数据并在列表视图中呈现数据的父组件 列表视图渲染组件 ListName 具有 onRowPress 函数 但 this props navigation navigate 未定义