question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any way to pass store to connect()?

See original GitHub issue

I’m trying to integrate React with a legacy Angular app, using https://github.com/ngReact/ngReact.

I’m using build tools (browserify, babel) for the React related code, and exporting components globally via window. The angular app is not using build tools so everything it needs will need to be in the global scope. Angular can render these component via the directive provided by ngReact, e.g. this directive renders the globally exported HelloComponent: <react-component name="HelloComponent"></react-component>

The thing is I want to use connect() on HelloComponent, but the question is then how do I specify the store for the connected component? Since there will be many React roots it seems like Provider isn’t a great idea. Passing the store as props to the connected component would have to be done in the angular world and it would be inconvenient.

The other thing I can think of is passing store during the call to connect, but the API doesn’t support that. Was there a reason for not allowing this way of passing the store?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

42reactions
gaearoncommented, May 24, 2016

You can create a custom connect():

function connectWithStore(store, WrappedComponent, ...args) {
  var ConnectedWrappedComponent = connect(...args)(WrappedComponent)
  return function (props) {
    return <ConnectedWrappedComponent {...props} store={store} />
  }
}

// Usage:
Stuff = connectWithStore(store, Stuff, mapStateToProps)
2reactions
jordanpapaleocommented, Mar 20, 2018

Hi Team

Little background: I need to have separate stores as I am building micro apps that exist in a container app. My parent component is an installable product uses redux and react-redux. It will be installed in other apps that have the same state management stack as well as install other micro apps that have their own state. It may sound a little confusing but in reality its pretty straight forward. It basically follows an installable widget pattern. All of these widgets are complex enough to need their own state and they need to be able to be installed in many different locations.

APP 1: needs isolated redux and react-redux WIDGET 1: Installed in APP 1 and needs isolated redux and react-redux WIDGET 2: Installed in WIDGET 1 and needs isolated redux and react-redux

Its all been going fine. In my lower lever widgets I have been using redux without react-redux but that breaks the patterns my entire team follows and introduces a slightly different paradigm for state management. I’d like to keep things consistent so I am updating my data flow to use react-redux for all apps and widgets.

The issue I have found is the nesting of application stores.

I have been having some issues with implementing this. I am using the same code block from Dan above:

image

Here is the implementation:

image

Here is the console output from the logs in connectWithStore function:

image

I am getting the following error:

Could not find "store" in either the context or props of "Connect(App)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(App)".

I did remove <Provider store={store}> from my application as I am explicitly passing the store via the connect.

I have tried a bunch of different angles but have not been able to make it past here. I would love some guidance please.

Thanks,

Jordan

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pass Store Explicitly Into as a Prop to Connect() in React
In this guide, you will learn how to create and pass the store as a prop to the connect() method of Redux. Passing...
Read more >
How to pass store explicitly into as a prop to "Connect()"
Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect()". The error comes while rendering the...
Read more >
React Redux connect(): When and how to use it
The React Redux connect() API is used for creating container elements that are connected to the Redux store. The Redux store is derived...
Read more >
Connecting React and Redux - Manning Publications
Any components rendered as children of Provider can access the Redux store. connect – a function used as a bridge between React components ......
Read more >
Connect | React Redux
The connect() function connects a React component to a Redux store. It provides its connected component with the pieces of the data it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found