我是React Native中的新手。我想创建一个具有React Native的应用程序,该应用程序将适用于Web和Mobile(iOS,Android(。
所以,为了解决我的问题,我将遵循链接。
https://reaect-native-training.github.io/reaect-native-elements/blog/2018/12/13/react-native-native-web.html
它在网络和移动设备上工作良好。
当我的项目想要在React Native中进行某些配置时,我正在使用 REECT Native Element [此处],并且在将此模块添加到我的项目中后运行良好。
现在,我在项目中增加了一个模块。它给了我错误。
我想添加 React Navigation 。因此,我更喜欢以下链接以添加React导航:https://reaectnavigation.org/docs/en/getting-started.html
每当我将该模块导入我的项目时,然后是链接。例如
import { createStackNavigator, createAppContainer } from "react-navigation";
它给了我以下错误:
./node_modules/@react-navigation/native/dist/ResourceSavingSceneView.js
SyntaxError: /home/firstproject/node_modules/@react-navigation/native/dist/
ResourceSavingSceneView.js: Unexpected token (33:11)
31 | } = this.props;
32 |
> 33 | return <View style={styles.container} collapsable={false}
removeClippedSubviews={Platform.OS === 'android' ?
removeClippedSubviews : !isFocused && removeClippedSubviews}>
| ^
34 | <View style={this._mustAlwaysBeVisible() || isFocused ?
styles.innerAttached : styles.innerDetached}>
35 | {awake ? <SceneView {...rest} navigation=
{childNavigation} /> : null}
36 | </View>
这是 Resourcesavingsceneview.js (它是预先编码的文件 React Navigation (
1. import React from 'react';
2. import { Platform, StyleSheet, View } from 'react-native';
3. import { SceneView } from '@react-navigation/core';
4.
5. const FAR_FAR_AWAY = 3000; // this should be big enough to move the whole view out of its container
6.
7. class ResourceSavingSceneView extends React.PureComponent {
8. static getDerivedStateFromProps(nextProps, prevState) {
9. if (nextProps.isFocused && !prevState.awake) {
10. return { awake: true };
11. } else {
12. return null;
13. }
14. }
15.
16. constructor(props) {
17. super();
18.
19. this.state = {
20. awake: props.lazy ? props.isFocused : true
21. };
22. }
23.
24. render() {
25. const { awake } = this.state;
26. const {
27. isFocused,
28. childNavigation,
29. removeClippedSubviews,
30. ...rest
31. } = this.props;
32.
33. return <View style={styles.container} collapsable={false} removeClippedSubviews={Platform.OS === 'android' ? false : !isFocused && false}>
34. <View style={this._mustAlwaysBeVisible() || isFocused ? styles.innerAttached : styles.innerDetached}>
35. {awake ? <SceneView {...rest} navigation={childNavigation} /> : null}
36. </View>
37. </View>;
38. }
39.
40. _mustAlwaysBeVisible = () => {
41. return this.props.animationEnabled || this.props.swipeEnabled;
42. };
43. }
44.
45. const styles = StyleSheet.create({
46. container: {
47. flex: 1,
48. overflow: 'hidden'
49. },
50. innerAttached: {
51. flex: 1
52. },
53. innerDetached: {
54. flex: 1,
55. top: FAR_FAR_AWAY
56. }
57. });
58.
59. export default ResourceSavingSceneView;
这是我的 package.json
{
"name": "firstproject",
"version": "0.1.0",
"private": true,
"scripts": {
"start": "./node_modules/.bin/react-native start",
"start-clean": "rm -rf $TMPDIR/react-* && watchman watch-del-all && npm start -- --reset-cache",
"android": "./node_modules/.bin/react-native run-android",
"ios": "./node_modules/.bin/react-native run-ios",
"web": "node scripts/start.js",
"build": "node scripts/build.js",
"test": "npm run test:web && npm run test:native",
"test:web-watch": "node scripts/test.js --watch --config "./config/web.jest.config.js"",
"test:web": "node scripts/test.js --config "./config/web.jest.config.js"",
"test:native": "node scripts/test.js --config "./config/native.jest.config.js"",
"test:native-watch": "node scripts/test.js --watch --config "./config/native.jest.config.js"",
"coverage": "node scripts/test.js --coverage --config "./config/web.jest.config.js"",
"coverage:native": "node scripts/test.js --config "./config/native.jest.config.js" --coverage"
},
"dependencies": {
"fetch-hoc": "^0.5.0",
"formik": "^1.5.1",
"react": "^16.6.x",
"react-app-polyfill": "^0.1.3",
"react-art": "^16.6.x",
"react-dom": "^16.6.x",
"react-native": "^0.59.6",
"react-native-elements": "^1.0.0-beta8",
"react-native-gesture-handler": "^1.2.1",
"react-native-vector-icons": "^6.4.1",
"react-native-web": "^0.10.x",
"react-navigation": "^3.9.1",
"react-router-dom": "^4.3.1",
"react-router-native": "^4.3.0",
"redux": "^4.0.1"
},
"devDependencies": {
"@babel/core": "7.1.0",
"@svgr/webpack": "2.4.1",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "9.0.0",
"babel-jest": "23.6.0",
"babel-loader": "8.0.4",
"babel-plugin-module-resolver": "^3.1.1",
"babel-plugin-named-asset-import": "^0.2.3",
"babel-preset-react-app": "^6.1.0",
"bfj": "6.1.1",
"case-sensitive-paths-webpack-plugin": "2.1.2",
"chalk": "2.4.1",
"css-loader": "1.0.0",
"dotenv": "6.0.0",
"dotenv-expand": "4.2.0",
"eslint": "5.6.0",
"eslint-config-react-app": "^3.0.5",
"eslint-loader": "2.1.1",
"eslint-plugin-flowtype": "2.50.1",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-jsx-a11y": "6.1.2",
"eslint-plugin-react": "7.11.1",
"file-loader": "2.0.0",
"fork-ts-checker-webpack-plugin-alt": "0.4.14",
"fs-extra": "7.0.0",
"html-webpack-plugin": "4.0.0-alpha.2",
"identity-obj-proxy": "3.0.0",
"jest": "23.6.0",
"jest-pnp-resolver": "1.0.1",
"jest-resolve": "23.6.0",
"metro-react-native-babel-preset": "^0.51.0",
"mini-css-extract-plugin": "0.4.3",
"optimize-css-assets-webpack-plugin": "5.0.1",
"pnp-webpack-plugin": "1.1.0",
"postcss-flexbugs-fixes": "4.1.0",
"postcss-loader": "3.0.0",
"postcss-preset-env": "6.0.6",
"postcss-safe-parser": "4.0.1",
"react-dev-utils": "^6.1.1",
"react-hot-loader": "^4.6.3",
"react-test-renderer": "^16.6.3",
"resolve": "1.8.1",
"sass-loader": "7.1.0",
"style-loader": "0.23.0",
"terser-webpack-plugin": "1.1.0",
"url-loader": "1.1.1",
"webpack": "4.19.1",
"webpack-dev-server": "^3.3.1",
"webpack-manifest-plugin": "2.0.4",
"workbox-webpack-plugin": "3.6.3"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
这是我的 babel.config.js
module.exports = function(api) {
api.cache(true);
return {
presets: [["module:metro-react-native-babel-preset"], ['react-app']],
ignore: [ "node_modules/art/core/color.js" ],
plugins: [
["module-resolver", {
"alias": {
"^react-native$": "react-native-web"
}
}]
],
};
};
如何解决我的问题。预先感谢。
您忘记了在行号33
上"返回"之后添加((您的观点需要在圆括号内,即
return (
<View style={styles.container} collapsable={false} removeClippedSubviews={Platform.OS === 'android' ? removeClippedSubviews : !isFocused && removeClippedSubviews}>
<View style={this._mustAlwaysBeVisible() || isFocused ? styles.innerAttached : styles.innerDetached}>
{awake ? <SceneView {...rest} navigation={childNavigation} /> : null}
</View>
</View>
)
我找到了一个解决方案,取自:
https://github.com/necolas/reacl-native-web/issues/1411#issuecomment-551169082
他指出了一个涉及该问题的示例应用程序:
https://github.com/possivetyler/rnnavwebappexample/blob/master/package.json
问题是WebPack与React-Native相比如何编译模块。他安装了一个hack工具(称为react-app-rewired(,该工具可以使用名为config-overrides.js的配置文件重新配置脚本。在文件中,您可以指定要重新编译的文件列表(不要问我如何准确地工作(。然后,在您的软件包中。
https://github.com/timarney/reeact-app-wreed
我现在仍在其他模块中遇到错误,但也许这将为您解决问题。
更新:我修复了这些错误,它们是无关的。现在对我有用!