Most examples assume that all of your html markup will live in either the root component or one of its children like this.
ReactDOM.render(
<Provider store={store}>
<MyRootComponent />
</Provider>,
rootEl
)
I'm working on a project where we are parsing an html page and looking for a custom html tag for example
<Component type="checkBox" id="blah" otherProp="someValue"> </Component>
If we find this tag we render a react component where this tag used to be. There can be many of these tags on an html page. This worked before no problem since we could just call render once per tag and it would replace it. Just to be clear, the entire page is NOT a react app we only render react components into these special placeholder tags and the rest of the page is just regular html.
Is it possible to do the same with react-redux? All the examples I have seen contain a single render at the highest level that has the provider as a wrapper. How could I accomplish multiple renders into specific placeholder tags but still benefit from a redux global store? I've looked into dangerouslySetInnerHtml but it seems to be frowned upon for security reasons. It also doesn't really accomplish what I want since it ends up rendering these placeholder tags directly into the html when what I really want to do is replace each one of these tags with a react component render. I've been stuck on this for awhile so any and all help would really be appreciated. Thank you.
Provider is very simple. It just passes the given store to the context. So you can have multiple providers on a page. Just give them the same instance of store.
const store = createStore(/* ... */)
// ...
ReactDOM.render(
<Provider store={store}>
<CheckBox />
</Provider>,
checkBoxTargetNode
)
ReactDOM.render(
<Provider store={store}>
<TextBox />
</Provider>,
textBoxTargetNode
)
Yep, that should work. @GrumpyPants , if you have more concerns beyond that let us know, but yeah, simply rendering multiple providers with the same store reference should do exactly what you want.
This is great to hear. I started looking for workarounds before even trying to do the multiple render so that's on me. Sorry for the time-waste and thanks for the help :)
Sure. Usage questions are generally better off on Stack Overflow rather than here, but it is admittedly a more niche use case you were asking about.
@GrumpyPants Do you have a public repo that shows how you implemented rendering of multiple <Provider> components for a shared store? More specifically, is there any way you've found to do this while still creating container components via { connect }?
Feel free to email me at todd.[email protected] as this is not so much a bug as a use case question like Mark stated.
@toddgroff : the example that @anatoliyarkhipov gave should be the basic idea. Create one store instance, then call ReactDOM.render() multiple times with your different component trees.
Hi I'm having the same issue I have 2 ReactDom.render() but for whatever reason when I do an action on component (A) component (B) does not update. What I'm trying to do is just turn a value from true to false. When I change the value, Component (A) reacts to it and updates but Component (B) stays the same.
const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);
import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {createStore, applyMiddleware} from 'redux';
import ReduxPromise from 'redux-promise';
import App from './components/app';
import SideImageComp from './components/SideImage/SideImageComp';
import reducers from './reducers';
const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<App/>
</Provider>, document.querySelector('.Approot'));
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<SideImageComp/>
</Provider>, document.querySelector('.sideImage'));
@jaysg Looks like you're creating 2 instances of your store instead of just one.
@jimbolla thanks thats what it was. I created a variable for the store and now it works.
just in case someone one sees this and it's going through the same thing.
import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {createStore, applyMiddleware} from 'redux';
import ReduxPromise from 'redux-promise';
import App from './components/app';
import SideImageComp from './components/SideImage/SideImageComp';
import reducers from './reducers';
const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);
// This is what I added
let newstore = createStoreWithMiddleware(reducers);
ReactDOM.render(
<Provider store={newstore}>
<App/>
</Provider>, document.querySelector('.Approot'));
ReactDOM.render(
<Provider store={newstore}>
<SideImageComp/>
</Provider>, document.querySelector('.sideImage'));
Provideris very simple. It just passes the givenstoreto thecontext. So you can have multiple providers on a page. Just give them the same instance ofstore.const store = createStore(/* ... */) // ... ReactDOM.render( <Provider store={store}> <CheckBox /> </Provider>, checkBoxTargetNode ) ReactDOM.render( <Provider store={store}> <TextBox /> </Provider>, textBoxTargetNode )
Is this still an OK approach in 2019?
@mattkoch614 : yes, nothing about that concept has changed at all.
This approach is getting me a Warning: Detected multiple renderers concurrently rendering the same context provider. This is currently unsupported.
Any ideas?
This approach is getting me a
Warning: Detected multiple renderers concurrently rendering the same context provider. This is currently unsupported.Any ideas?
Same error for me, when I try to render lots of components on page wrapped inside web components.
Can you create a CodeSandbox that demonstrates the issue?
Found a reason, I was rendering react components wrapped in web components from my external package both with connected react components inside one app.
Most helpful comment
Provideris very simple. It just passes the givenstoreto thecontext. So you can have multiple providers on a page. Just give them the same instance ofstore.