React原生-需要循环

ycl3bljg  于 2023-02-09  发布在  React
关注(0)|答案(1)|浏览(165)

∮ ∮我面对的是:
我有一个小型RN项目,在实现了全局状态管理的一些基本逻辑之后,当我启动/重新启动我的项目时,我得到了这个错误:

Require cycle: App.js -> src\navigation.js -> src\components\Drawer.js -> App.js

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

Require cycle: App.js -> src\navigation.js -> src\pages\signIn.js -> App.js

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

Require cycle: App.js -> src\navigation.js -> App.js

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

我写了我的代码基于this guide从React导航文档与一些小的变化,它的工作与预期的条件渲染和所有,但我不断得到这个恼人的错误,我真的想摆脱它。

我的密码:

    • 应用程序js**
//Useless imports
import Navigator from './src/navigation';
import appReducer from './src/reducers';

export const AuthContext = createContext();

const App = () => {
  const [state, dispatch] = useReducer(appReducer, initialStates);

  //useEffect to fetch stored credentials and change initialStates
  //for conditional rendering purposes
 
  return (
    <AuthContext.Provider value={[state, dispatch]}>
      <Navigator/>
    </AuthContext.Provider>
  );
}

export default App;
    • 导航. js**
//Useless imports
import { AuthContext } from '../App';
import SignIn from './pages/SignIn';
import Drawer from './components/Drawer'; //Used in my Drawer Navigator declaration,
                                          //but I'll abstract some of my nav logic

export const Navigator = () => {
  const [state, dispatch] = useContext(AuthContext);

  return (
    <NavigationContainer>
      {
        state.someState ?
          <Screen1 component={SignIn} />
          :
          <Screen2 component={Screen2} />
      }
    </NavigationContainer>
  );
}
    • 抽屉**
///Useless imports
import { AuthContext }  from '../../App';

export const Drawer = () => {
  const [state, dispatch] = useContext(AuthContext);

  return (
    <Drawer>
      <Button onPress={() => dispatch({type: 'SIGN_OUT'})}/>
    </Drawer>
  );
}
    • 登录. js**
//Useless imports
import { AuthContext } from '../../App';

export const SignIn = () => {
  const [state, dispatch] = useContext(AuthContext);

  return (
    <View>
      <Button onPress={() => dispatch({type: 'SIGN_IN'})/>
    </View>
  );
}

我真的不认为我的reducer声明或任何类似的东西有问题,因为正如我之前所说的,条件渲染按预期工作,当我从组件或页面注解AuthContext导入时,警告消失了。
以下是我的文件夹结构,仅供参考:

My-App/
├── src/
│   ├── components/
│   │   └── Drawer.js
│   ├── pages/
│   │   └── SignIn.js
│   ├── reducers/
│   │   └── appReducer.js
│   └── navigation.js
└── App.js
wlzqhblo

wlzqhblo1#

警告正确;由于您是从App导出AuthContext,然后将其导入到嵌套组件中,因此您已经创建了一个循环。最简单的方法是将AuthContext移动到由App和登录页面导入的单独文件中。

相关问题