1
votes

I know there are a lot of similar posts, but none of them seem to be similar to this one.

Using react redux to use mapStateToProps.

in my App.js I use import Score from './components/Score'; in order to import the component.

The component is in a folder called Score, where it contains index.js and Score.js. Inside Score.js I have:

const Score = ({team_score}) => ( 
    <>  
        <p>{ team_score }</p>
    </>
);

export default Score;

Inside my index.js I have:

import { connect } from "react-redux";
import Score from "./Score";

let mapStateToProps = (state) => {
    return {
        team_score: state.team_score,
    };
};

connect(mapStateToProps)(Score)

But I get the error:

./src/App.js
Attempted import error: './components/Score' does not contain a default export (imported as 'Score').

I have triple checked and the file path is correct. I'm not sure why I'm getting this error.

2
I cannot clearly see an issue there, could you please reproduce all the above in a stackblitz project so that we can work out what's going wrong? :) - Dario Piotrowicz

2 Answers

0
votes

I think your problem is you have index.js file inside this folder.

import Score from './components/Score'

This line will try to import anything from components/Score/index.js file and you do not export anything from there.

You should do something like below:

import { connect } from "react-redux";
import Score from "./Score";

let mapStateToProps = (state) => {
    return {
        team_score: state.team_score,
    };
};

export default connect(mapStateToProps)(Score)
0
votes

Two solutions :

  1. Export Score from index.js file inside Score folder. (after importing from Score file)
  2. Delete index.js and use import Score from './components/Score/Score'; in App.js

Better organized way is 1 if you are returning just one value from a module.

Reason for error : You are importing value from Score folder. When value is imported from folder, index.js in that folder returns the value. But here index.js is not exporting any value. So either export value from index.js (solution 1) or import value from Score file inside Score folder (solution 2)