0

Hello I am working on Shopping List and my file looks like this:

import React, { Component } from 'react';
import AppNavbar from './components/AppNavbar';
import ShoppingList from './components/ShoppingList';

import {Provider} from 'react-redux';
import store from './';
import 'bootstrap/dist/css/bootstrap.min.css';
import './App.css';

function App() {
  return (
    <Provider store={store}>
    <div className="App">
     <AppNavbar/> 
     <ShoppingList/>
    </div>
    </Provider>
  );
}

export default App;

However, Ireceived this error: Failed to compile ./src/App.js Attempted import error: './' does not contain a default export (imported as 'store').

can someone please help fix this ?

1
  • 1
    do you have an index.js file in the same directory as your shopping list component? Commented Apr 15, 2020 at 16:26

2 Answers 2

1

The problem in your code is how you are trying load the store with import store from './';.

Instead you need to use createStore() from redux as the following:

import { createStore, applyMiddleware } from 'redux';

const store = createStore(
    /* your reducers */,
    applyMiddleware( /* your middleware */ ),
);

What you can pass to the <Provider /> as follows:

ReactDOM.render(<Provider store={store}>
                    <App />
                </Provider>, document.getElementById('root'));

Read further here about createStore(reducer, \[preloadedState\], \[enhancer\]) which:

Creates a Redux store that holds the complete state tree of your app. There should only be a single store in your app.

I hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0
import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';

import reducers from './reducers';

import Posts from './containers/Posts'
import NavBar from './components/NavBar';

// Apply Thunk middleware
const middleware = applyMiddleware(thunk);
// Create store
const store = createStore(reducers, enhancer);

class App extends Component {
 render() {
    return (
  <Provider store={store}>
      <React.Fragment>
        <NavBar />
        <Posts />
      </React.Fragment>
  </Provider>
);

As you can see, the app.js page should be looked like this. You can't just import store from another file. It's better to use best practices like this. Now you will wonder how the reducers came from. Generally, developers put all the reducers in a reducers folder. In that folder you can have a index.js file and other reducer files. For now, you better use a empty main reducer index.js like below.

 import { combineReducers } from 'redux'

 const rootReducer = combineReducers({
      null
  })

  export default rootReducer;

If you something like post or whatever, the code should be modified like this.

import { combineReducers } from 'redux'

import posts from './posts';

const rootReducer = combineReducers({
    posts: posts
 })

 export default rootReducer;

Simply, posts is a post reducer in the reducers folder. Selecting a appropriate middleware is up to you. I have used redux-thunk. You can use redux-saga, redux-logic, or custom middleware. I think this will help you to solve your problem.

1 Comment

Thank you very much. I used your code. Can you please help me handle this new feedback: Failed to compile ./src/App.js Line 25:3: Parsing error: Unexpected token 23 | </React.Fragment> 24 | </Provider> > 25 | ); | ^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.