简体   繁体   中英

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.

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 ) :

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')

)

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 . The code you shared in the reducer file does not have any functions/variables/expressions defined/named as movieReducer .

Another option would be to just change const establishmentsReducer = (state = initialState, action) => { to const movieReducer = (state = initialState, action) => { .

Hopefully that helps!

Bro you have import correctly:

import you reducer like :

import { allReducers } from './reducers';

This will solve your Problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM