Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React Native-动态列出/要求目录中的文件_Javascript_React Native_Redux_React Redux - Fatal编程技术网

Javascript React Native-动态列出/要求目录中的文件

Javascript React Native-动态列出/要求目录中的文件,javascript,react-native,redux,react-redux,Javascript,React Native,Redux,React Redux,我正在使用Redux,希望动态地包含目录中的所有文件 /redux/index.js // Actions import * as authActions from './auth/authActions'; import * as deviceActions from './device/deviceActions'; import * as globalActions from './global/globalActions'; import * as menuActions from '

我正在使用Redux,希望动态地包含目录中的所有文件

/redux/index.js

// Actions

import * as authActions from './auth/authActions';
import * as deviceActions from './device/deviceActions';
import * as globalActions from './global/globalActions';
import * as menuActions from './menu/menuActions';
... etc

export const actions = [
  authActions,
  deviceActions,
  globalActions,
  menuActions,
...
];

// Reducers

import auth from './auth/authReducer';
import device from './device/deviceReducer';
import global from './global/globalReducer';
import menu from './menu/menuReducer';
...

import { combineReducers } from 'redux';

export const rootReducer = combineReducers({
  auth,
  device,
  global,
  menu,
...
});
在上述(简化)示例中,所有文件的结构如下:

/redux/
  /auth/
    authActions.js
    authReducer.js
  /device/
    deviceActions.js
    deviceReducer.js
  /global/
    globalActions.js
    globalReducer.js
  /menu/
    menuActions.js
    menuReducer.js
  ...
在这个index.js文件中,如果我能够动态读取redux目录中的所有目录,并动态地要求导出操作和还原程序,那么维护起来就会容易得多

在常规节点环境中,我将执行以下操作(未测试,但演示了示例):

问题在于,
fs
模块在react native中不是一个能够动态迭代代码库中的目录的东西。有react native fs,但这是为了实际访问设备上的文件系统(在编译应用程序之后)[我想是吧]。上述方法比单独要求所有动作和减缩器,并在动作数组和减缩器对象中指定它们要干净得多


有什么想法吗?

react native不支持动态加载模块。所有javascript文件都捆绑到一个js文件中

import fs from 'fs'
import path from 'path'
import { combineReducers } from 'redux'

let actions = []
let reducers {}

fs
  .readdirSync(__dirname).filter((file) => {
    // Only directories
    return fs.statSync(path.join(__dirname, file)).isDirectory();
  })
  .forEach((module) => {
    const moduleActions = require(path.join(__dirname, module, `${module}Actions.js`);
    const moduleReducer = require(path.join(__dirname, module, `${module}Reducer.js`);

    actions.push(moduleActions)
    reducers[module] = moduleReducer.default
  });

export actions
export const rootReducer = combineReducers(reducers)