Require cycles are allowed, but can result in uninitialized values. Consider refactoring to remove the need for a cycle

后端 未结 8 1705
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-24 10:12

I am receiving this warning message in my chrome console for my react-native project. Do you have any idea why I am getting this?

This is the complete message:

相关标签:
8条回答
  • 2020-12-24 10:52

    In my case the warning was like this;

    Require cycle: src\views\TeamVerification.js -> src\components\TeamVerificationListItem.js -> src\views\TeamVerification.js Require cycles are allowed, but can result in uninitialized values. Consider refactoring to remove the need for a cycle.

    As it indicates, TeamVerification was importing TeamVerificationListItem and TeamVerificationListItem was also importing TeamVerification. It was an unused import but after I remove it the warning gone.

    0 讨论(0)
  • 2020-12-24 10:57

    To prevent from having to write multiple lines of

    import SomeComponent from "../components"
    import AnotherComponent from "../components"
    import AndAnotherComponent from "../components"
    import AndOneMoreComponent from "../components"
    

    I created a comp.js file where I could import the components as they are created and export them as modules. All components are then able to be reached from one place. So you can then have something like this in some place...

    import { SomeComponent, AnotherComponent, AndAnotherComponent, AndOneMoreComponent} from './comp'
    

    Now what happens in the renderer for example when SomeComponent is rendered....

    import * as React from "react";
    import { AnotherComponent} from '../comps';
    import { View, Text } from "react-native";
    
    function SomeComponent() {
    return (
        <>
        <AnotherComponent />
        <View><Text>EXAMPLE OF SOMECOMPONENT</Text></View>
        </>
    )
    }
    export default SomeComponent;
    

    In the example, SomeComponent could be called in the main App, and when it renders it also asks for a component from the comp.js This is what triggers the Require cycle warning because a module that was imported from one place, is then rendering and asking to import another module from the same place it was rendered from.

    What are your thoughts on this, should I revert back to using single import statements or do you think there is a danger in using the module export as it is currently setup?

    0 讨论(0)
  • 2020-12-24 10:59

    In my case, i had the same warning after the installation of a 'package'

    and in their documentation, it was import SomeFunc from 'package'

    and instantly the warning showed up

    Require cycles are allowed but can result in uninitialized values. Consider refactoring to remove the need for a cycle.

    but as soon as I destructure the SomeFunc there was no more warning

    import {SomeFunc} from 'package'

    please look at the destructuring

    0 讨论(0)
  • 2020-12-24 11:00

    I my case, I have sold the same problem in react-native navgiation.

    What I did ?

    Already I was using react-navigation like below

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

    and then I was consuming it like:

    import {containerRef} from 'filename';
    
    onPress = ()=> containerRef.current.navigate('Chat');
    

    But I updated like below and warning has gone.

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

    and then I was consuming it like:

    import { useNavigation } from '@react-navigation/native';
    
    onPress = ()=> useNavigation.navigate('Chat');
    
    0 讨论(0)
  • 2020-12-24 11:01

    You are probably importing something from "file A" into "file B", then importing something again from "file B" into "file A" . Examine all the imports from both the files and see if there's any such cycle.

    0 讨论(0)
  • 2020-12-24 11:07

    TL;DR: You import module A into module B and module B into module A resulting in a cycle A

    0 讨论(0)
提交回复
热议问题