我开始学习React + Redux,我正在做一个简单的应用程序来添加或删除购物篮中的电影。
但是我在减速机上有问题。我尝试很多事情都没有结果...
提前致谢 :)
这是我的代码:
减速器:
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;
减速器的连接(我知道这不是没有用,但是将来必须结合使用减速器):
import { combineReducers } from 'redux';
import movieReducer from './movieReducer';
const allReducers = combineReducers({
movie : movieReducer
})
export default allReducers;
index.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')
)
最佳答案
在减速器中,更改:
export default movieReducer;
至:
export default establishmentsReducer;
这是因为您需要导出实际定义的函数或变量,在本例中为
establishmentsReducer
。您在化简文件中共享的代码没有定义/命名为movieReducer
的任何函数/变量/表达式。另一种选择是将
const establishmentsReducer = (state = initialState, action) => {
更改为const movieReducer = (state = initialState, action) => {
。希望有帮助!
关于javascript - 尝试导入错误:“./movieReducer”不包含默认导出(导入为“movieReducer”),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53339570/