[英]Attempted import error: './movieReducer' does not contain a default export (imported as 'movieReducer')
I'm starting learn React + Redux , I'm doing a simple application for add or remove a movie in a basket.我开始学习 React + Redux ,我正在做一个简单的应用程序,用于在篮子中添加或删除电影。
But I have a problem on a reducer .但是我在减速器上有问题。 I try a lot of things without result...
我尝试了很多事情都没有结果......
Thanks in advance :)提前致谢 :)
Here is my code :这是我的代码:
The reducer :减速机:
import '../actions/actionsTypes';
import { movies } from '../components/movie/data.json';
let initialState = []
movies.map((movie) => {
initialState.push({
id : movie.id,
title: movie.title,
year: year.title,
isAdd: false,
isRemove: false
})
return movie
})
const establishment = (state = {}, action) => {
switch (action.type) {
case ADD :
if (state.id !== action.data.id)
return state
return (
...state,
isAdd : !state.isAdd
)
case REMOVE :
if (state.id !== action.data.id)
return state
return (
...state,
isRemove : !state.isRemove
)
default:
return state
}
}
const establishmentsReducer = (state = initialState, action) => {
switch (action.type) {
case ADD :
return state.map(movieState =>
movie(movieState, action)
)
case REMOVE :
return state.map(movieState =>
movie(movieState, action)
)
default:
return state
}
}
export default movieReducer;
The connection of the reducer ( I know it's not useless but in the furtur will have to combine reducer ) :减速器的连接(我知道它不是没用,但在furtur将不得不结合减速器):
import { combineReducers } from 'redux';
import movieReducer from './movieReducer';
const allReducers = combineReducers({
movie : movieReducer
})
export default allReducers;
index.js :索引.js:
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import allReducers from './reducers';
import './index.css';
import App from './components/App';
import * as serviceWorker from './serviceWorker';
const store = createStore(allReducers);
ReactDOM.render(
<Provider store={ store }>
<App/>
</Provider>,
document.getElementById('root')
)
In your reducer, change:在您的减速器中,更改:
export default movieReducer;
To:到:
export default establishmentsReducer;
This is because you need to export an actually defined function or variable, in this case establishmentsReducer
.这是因为您需要导出一个实际定义的函数或变量,在本例中为
establishmentsReducer
。 The code you shared in the reducer file does not have any functions/variables/expressions defined/named as movieReducer
.您在 reducer 文件中共享的代码没有定义/命名为
movieReducer
任何函数/变量/表达式。
Another option would be to just change const establishmentsReducer = (state = initialState, action) => {
to const movieReducer = (state = initialState, action) => {
.另一种选择是只改变
const establishmentsReducer = (state = initialState, action) => {
到const movieReducer = (state = initialState, action) => {
。
Hopefully that helps!希望这有帮助!
Bro you have import correctly:兄弟,您已正确导入:
import you reducer like :导入您的减速器,例如:
import { allReducers } from './reducers';从 './reducers' 导入 { allReducers };
This will solve your Problem.这将解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.