Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 测试这个.props.navigation 从“React”导入React; 从“../Components/ListElements”导入ListElements; 类HospitalsScreen扩展了React.Component{ 建造师(道具){ 超级(道具); 此.state={ 医院:[“基地医院”、“塔瓜廷加地区医院”、“塞兰迪亚医院”、“克利尼卡医院”] }; } onPressItem(){ this.props.navigation.navigate('sections'); } render(){ 返回( ); } } 导出默认医院屏幕;_Javascript_React Native_Testing_Jestjs_Enzyme - Fatal编程技术网

Javascript 测试这个.props.navigation 从“React”导入React; 从“../Components/ListElements”导入ListElements; 类HospitalsScreen扩展了React.Component{ 建造师(道具){ 超级(道具); 此.state={ 医院:[“基地医院”、“塔瓜廷加地区医院”、“塞兰迪亚医院”、“克利尼卡医院”] }; } onPressItem(){ this.props.navigation.navigate('sections'); } render(){ 返回( ); } } 导出默认医院屏幕;

Javascript 测试这个.props.navigation 从“React”导入React; 从“../Components/ListElements”导入ListElements; 类HospitalsScreen扩展了React.Component{ 建造师(道具){ 超级(道具); 此.state={ 医院:[“基地医院”、“塔瓜廷加地区医院”、“塞兰迪亚医院”、“克利尼卡医院”] }; } onPressItem(){ this.props.navigation.navigate('sections'); } render(){ 返回( ); } } 导出默认医院屏幕;,javascript,react-native,testing,jestjs,enzyme,Javascript,React Native,Testing,Jestjs,Enzyme,我在测试这个屏幕的onPress功能时遇到了一些问题。 我没有找到任何解决问题的方法。在Javascript中,函数中this的值取决于该函数的调用方式 了解这个绑定在JavaScript中是如何工作的 有几种方法可以实现它。使用它们并查看是否将此作为未定义的函数 一个是添加this.onPressItem=this.onPressItem.bind(this) 另一个是箭头函数 onPressItem=(事件)=> {this.props.navigation.navigate('sector

我在测试这个屏幕的onPress功能时遇到了一些问题。
我没有找到任何解决问题的方法。

Javascript中,函数中
this
的值取决于该函数的调用方式

了解
这个
绑定在JavaScript中是如何工作的

有几种方法可以实现它。使用它们并查看是否将
作为未定义的函数

一个是添加
this.onPressItem=this.onPressItem.bind(this)

另一个是箭头函数

onPressItem=(事件)=>
{this.props.navigation.navigate('sectors');
}

然后是
onPress={this.onPressItem.bind(this)}

你可以找到更多的细节


另外,请仔细阅读这篇文章

错误是什么?您可以发布导航方法或用于导航的库名称吗?我正在使用react导航。问题是我找不到一种方法来测试这个,它总是给出错误“无法读取未定义的属性props”。我想这可能对你有帮助
import React from 'react';
import ListElements from '../Components/ListElements';
class HospitalsScreen extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      Hospitals: ['Hospital de Base','Hospital Regional de Taguatinga','Hospital da Ceilandia','Clínica']
    };
  }

  onPressItem() {
    this.props.navigation.navigate('sectors');
  }

  render() {
    return (
      <ListElements
      list = {this.state.Hospitals}
      title='Hospitais'
      onPress={this.onPressItem.bind(this)}
      />
    );
  }
}

export default HospitalsScreen;