0

For example my application consists from 2 parts and is wrapped in Provider:

<>
  <Provider store={store}>
     <ModalWindow />
     <Main />
  </Provider>
</>

I render different content in ModalWindow, for this I have config file:

export const modalConfig = {
  firstConfig: {
    onRender: (node, props) => {
      ReactDOM.render(<FirstComponent {...props} />, node)
    },
  },
  secondConfig: {
    onRender: (node, props) => {
      ReactDOM.render(<SecondComponent {...props} />, node)
    },
  },
 }

My Main component has always react-redux context and I can use useSelector or useDispatch

But why components which I render with ReactDOM.render() don't have react-redux context? I need to wrap them additional to solve this, like this:

export const modalConfig = {
  firstConfig: {
    onRender: (node, props) => {
      ReactDOM.render(<Provider store={store}><FirstComponent {...props} /></Provider>, node)
    },
  },
  secondConfig: {
    onRender: (node, props) => {
      ReactDOM.render(<Provider store={store}><SecondComponent {...props} /></Provider>, node)
    },
  },
}

Why it happens?

Дядя Фея
  • 359
  • 2
  • 12

1 Answers1

0

You are using ReactDOM.render to insert the modals into the document, so they wind up outside of the Provider component, which is rendered inside your root node (probably document.getElementById("root")).

I'm not sure why you need ReactDOM.render here. You could probably redesign this so that ModalWindow just conditionally returns some content. You can use CSS styling to make it into an overlay. You can also look into the React Portal API.

Linda Paiste
  • 38,446
  • 6
  • 64
  • 102