javascript - 运行反应原生项目时未定义不是函数(评估't.createContext(!1)')
问题描述
我编写了一个移动应用程序,通过使用 react-native 从用户那里收集一些数据。它使用react native 0.57.4
, react 16.2.0
, react-redux 5.0.5
, prop-types 15.5.10,
, react-navigation 1.0.0-beta.11
. 我还使用这个库在 web 视图中创建调查问题,即基本上嵌套一个包含 SurveyJS 调查的 web 视图在 React Native 应用程序中。
Package.JSON
{
"name": "SurveyJSInWebViewWithReactAndReactNative",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"lodash": "^4.17.11",
"moment": "^2.20.1",
"prop-types": "15.5.10",
"react": "16.2.0",
"react-native": "^0.57.4",
"react-native-webview-bridge": "git+https://github.com/abolger/react-native-webview-bridge.git",
"react-navigation": "1.0.0-beta.11",
"react-redux": "5.0.5",
"redux": "3.7.1"
},
"devDependencies": {
"babel-jest": "22.1.0",
"babel-preset-react-native": "5.0.2",
"jest": "22.1.2",
"native-base": "^2.8.1",
"react-test-renderer": "16.2.0"
},
"jest": {
"preset": "react-native"
}
}
app.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View
} from 'react-native';
import surveys from './survey/default_surveys';
import Survey from './survey';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
export default class App extends Component<{}> {
onSurveyCompleted(ownProps, nextProps) {
//TODO: Readers hooks go here.
alert("Nested Survey was completed!");
}
render() {
return (
<Survey
surveyJSON={surveys.example3PagePatientSatisfactionSurvey}
title="My Custom Survey Title"
navigation={this.props.navigation}
surveyResponseDateString={''}
onSurveyComplete={this.onSurveyCompleted}
data={{}}/>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
解决方案
唯一缺少的是与上下文绑定,否则我看不出您的代码有任何问题。您可以尝试将 onSurveyCompleted 函数与您的上下文绑定。这应该适合你
onSurveyComplete={this.onSurveyCompleted.bind(this)}
要么您必须在构造函数中绑定方法,要么在渲染中使用该方法。
constructor(){
this.onSurveyCompleted = this.onSurveyCompleted.bind(this);
}
推荐阅读
- mysql - Mysql In 运算符,python 格式
- azure - 将文件传输到 Azure 虚拟机的最佳方式是什么?
- java - Elasticsearch:java.lang.OutOfMemoryError 后重启节点:Java 堆空间
- javascript - 如何清理html中的css和js
- elasticsearch - mysql 到 Elasticsearch:按使用选定字段分组和排序
- angular - 类型字符串不可分配给类型编号
- c# - 如何在 Blazor 服务器应用程序中访问 WCF 服务
- pyspark - PySpark。如何确保每日增量数据在 HIVE 中没有重复的 UUID 作为 PK
- r - 从具有许多文本元素的数据框列中提取部分字符串(没有正则表达式)
- php - 在后台运行 PHP 函数/脚本?