0

I am very newbie with React and React native.

I have a folder with several components, I've been able to create an index.js file to export all components inside the folder, so in other components I can do:

# /components/OneComponent.js
...
export { OneComponent }

# index.js
import { OneComponent } from './components'
```

Now I am working with Redux, and I want to accomplish the same thing, but I don't know how to do it. In my component I export the component as follows:

# /component/OneComponent.js
export default connect(mapStateToProps, { myFunction})(OneComponent);

And it should be as follows as I guess

export { connect(mapStateToProps, { myFunction})(OneComponent) };

But it doesn't compile.

2
  • 1
    Have you tried export default connect(mapStateToProps, myFunction)(OneComponent); Commented May 23, 2017 at 13:59
  • Yes, If I do that, when I do import { OneComponent } from './components' it is undefined. Commented May 23, 2017 at 14:06

3 Answers 3

1

Just tested it out.

You can work around your error by assigning the result of the connect call to a const and then export that const afterward like so.

const comp1 = connect(mapStateToProps)(MyComponent);

export {comp1 as MyComponent};
Sign up to request clarification or add additional context in comments.

Comments

0

When exporting your components from an index you need to export them like this:

export {connect(mapStateToPops,mapDispatchToProps)(ComponentName)}

and then in your index.js: export * from 'YourComponentRoute

Remember that if you export as default you are exporting an instance of an unique object. If you export {} you are exporting a "copy" of an object.

1 Comment

Hi, if I do this export in the component js file, I get a javascript error at the parenthesis of connect that says ", expected". export { connect(mapStateToProps, { getSchedules })(SchedulesScreen) };
0

I dont think the problem is the Redux, the import and export ES6 methods are messy. For example:

  • Example 1 (with export default):

When you export default something, you should not import with {}

## CORRECT
# /components/OneComponent.js
...
export default connect(mapStateToProps, { myFunction})(OneComponent);

# index.js
import AnyName from './components/OneComponent.js'

## WRONG
# /components/OneComponent.js
...
export default connect(mapStateToProps, { myFunction})(OneComponent);

# index.js
import { OneComponent } from './components/OneComponent.js'
  • Example 2 (without export default):

Try to name your object keys

##CORRECT
# /components/OneComponent.js
...
export { OneComponent: connect(mapStateToProps, { myFunction})(OneComponent) };

# index.js
import { OneComponent } from './components'

##WRONG
# /components/OneComponent.js
...
export { connect(mapStateToProps, { myFunction})(OneComponent) };

# index.js
//It wont find the key OneComponent on exported object
import { OneComponent } from './components'

2 Comments

The first example is what I am doing right now. The second example doesn't work for me, I receive an error in the two dots that says ", expected" export { SchedulesScreen: connect(mapStateToProps, { getSchedules })(SchedulesScreen) };
Hi @Robsonsjre , I've created a github repo with the base code, could you check it? Thank you! github.com/davidrojo/test-import

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.