允许需要循环,但可能导致未初始化的值

时间:2019-06-06 14:58:29

标签: react-native

我刚开始使用本机反应,并且想要测试反应导航,这是我得到的错误。需要帮助

Require cycle: src\HomeScreen\index.js -> src\ChatScreen\index.js -> src\ChatScreen\LucyChat.js -> src\HomeScreen\index.js

允许使用要求周期,但可能导致未初始化的值。考虑重构以消除循环的需要。

- node_modules\react-native\Libraries\YellowBox\YellowBox.js:67:8 in warn
- node_modules\expo\build\environment\muteWarnings.fx.js:17:23 in warn
- node_modules\metro\src\lib\polyfills\require.js:115:8 in metroRequire
* src\ChatScreen\LucyChat.js:4:0 in <unknown>
- node_modules\metro\src\lib\polyfills\require.js:331:6 in loadModuleImplementation
* src\ChatScreen\index.js:2:0 in <unknown>
- node_modules\metro\src\lib\polyfills\require.js:331:6 in loadModuleImplementation
* src\HomeScreen\index.js:3:0 in <unknown>
- node_modules\metro\src\lib\polyfills\require.js:331:6 in loadModuleImplementation
* App.js:3:0 in <unknown>
- node_modules\metro\src\lib\polyfills\require.js:331:6 in loadModuleImplementation
- node_modules\expo\AppEntry.js:4:0 in <unknown>
- node_modules\metro\src\lib\polyfills\require.js:331:6 in loadModuleImplementation
- node_modules\metro\src\lib\polyfills\require.js:197:45 in guardedLoadModule
* null:null in global codee

2 个答案:

答案 0 :(得分:0)

刚遇到这个问题,就紧随answer

**package.json**

"scripts": {
    "postinstall": "node ./scripts/stfu.js", }

**/scripts/stfu.js**

const fs = require('fs');

const codeToObscure = /console.warn\('Require cycle: '/gi; const problemFilePath = './node_modules/metro/src/lib/polyfills/require.js'; const problemFileContent = fs.readFileSync(problemFilePath, 'utf8'); fs.writeFileSync(   problemFilePath,   problemFileContent.replace(codeToObscure, 'const noConsoleWarn = (""'),   'utf8', );

然后做

npm install

or

yarn

希望这会有所帮助!

答案 1 :(得分:0)

我在本机反应中也遇到了同样的问题。

我做了什么?

我已经像下面那样使用react-navigation

 export const containerRef = createRef();

 function App(){
   return (
     <NavigationContainer ref={containerRef}>
       ....
     <NavigationContainer>
   );
 }

然后我像这样食用它:

import {containerRef} from 'filename';

onPress = ()=> containerRef.current.navigate('Chat');

但是我如下更新,警告消失了。

 function App(){
   return (
     <NavigationContainer> // removed ref
       ....
     <NavigationContainer>
   );
 }

然后我像这样食用它:

import { useNavigation } from '@react-navigation/native';

onPress = ()=> useNavigation.navigate('Chat');