gpt4 book ai didi

javascript - 尝试导入错误 : './movieReducer' does not contain a default export (imported as 'movieReducer' )

转载 作者:行者123 更新时间:2023-11-30 20:01:23 25 4
gpt4 key购买 nike

我开始学习 React + Redux,我正在做一个简单的应用程序来添加或删除篮子里的电影。

但是我在 reducer 上遇到了问题。我尝试了很多事情都没有结果......

提前致谢:)

这是我的代码:

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;

reducer 的连接(我知道不是没用但是以后要结合reducer):

import { combineReducers } from 'redux';

import movieReducer from './movieReducer';


const allReducers = combineReducers({
movie : movieReducer
})

export default allReducers;

索引.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')

)

最佳答案

在你的reducer中,改变:

export default movieReducer;

收件人:

export default establishmentsReducer;

这是因为您需要导出实际定义的函数或变量,在本例中为 establishmentsReducer。您在 reducer 文件中共享的代码没有定义/命名为 movieReducer 的任何函数/变量/表达式。

另一种选择是将 const establishmentsReducer = (state = initialState, action) => { 更改为 const movieReducer = (state = initialState, action) => {.

希望对您有所帮助!

关于javascript - 尝试导入错误 : './movieReducer' does not contain a default export (imported as 'movieReducer' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53339570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com