我们如何默认导入index.js以及不是index.js文件中的reducer

时间:2019-06-11 11:00:22

标签: reactjs redux

我的代码中没有类或函数化简器,但解释说index.js默认使用index.js,有类或函数名为化简器,我没有错误,并且如何正确编译?

我已使用create-react -app创建应用程序

以下是index.js代码,它是化简器

import { combineReducers } from 'redux';
import movies from './movies_reducer';

const rootReducer =  combineReducers({
    movies
})

export default rootReducer

以下是我使用的来自reducer的index.js的index.js文件

import reducers from './reducers'

const   createStoreWithMiddleware = applyMiddleware()(createStore)

ReactDOM.render(

<Provider store={createStoreWithMiddleware()}>
<App />
</Provider>,

上面的代码如何工作,请解释

1 个答案:

答案 0 :(得分:0)

导入目录是在目录内导入index.js文件的语法手段。假设其中有一个名为reducers的目录和一个名为index.js的文件,则以下导入是等效的。

import reducers from './reducers'
import reducers from './reducers/index'
import reducers from './reducers/index.js'

此外,可以使用任何名称导入默认导出。

export default rootReducer

// file.js - the following import statements import the same rootReducer
import reducers from './reducers'
import rootReducer from './reducers'
import anyName from './reducers'