Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass context into a component instantiated with ReactDOM.render?

TL;DR Given the following example code:

ReactDOM.render(<MyComponent prop1={someVar} />, someDomNode);

Is it possible to manually pass React context into the instance of MyComponent?

I know this sounds like a weird question given React's nature, but the use case is that I'm mixing React with Semantic UI (SUI) and this specific case is lazy-loading the contents of a SUI tooltip (the contents of the tooltip is a React component using the same code pattern as above) when the tooltip first displays. So it's not a React component being implicitly created by another React component, which seems to break context chain.

I'm wondering if I can manually keep the context chain going rather than having components that need to look for certain data in context AND props.

React version: 0.14.8

like image 505
webbower Avatar asked Jul 05 '16 20:07

webbower


People also ask

Which component do we have to pass to the ReactDOM render method?

render() The first argument is the element or component we want to render, and the second argument is the HTML element (the target node) to which we want to append it. Generally, when we create our project with create-react-app , it gives us a div with the id of a root inside index.

What does the ReactDOM render () method do?

The ReactDOM. render() function takes two arguments, HTML code and an HTML element. The purpose of the function is to display the specified HTML code inside the specified HTML element.

Does context change trigger re render?

It means that on every render of the App, the context value changes even if a and b don't. And when the context value changes, the context consumer re-renders. To prevent that, we need to make sure that the text variable receives the same object reference.

Can we have two ReactDOM render?

Answer. We can have multiple calls to ReactDOM. render() in a single JavaScript file without issue - having multiple calls to ReactDOM.


2 Answers

No. Before react 0.14 there was method React.withContext, but it was removed.

However you can do it by creating HoC component with context, it would be something like:

import React from 'react';

function createContextProvider(context){
  class ContextProvider extends React.Component {
    getChildContext() {
      return context;
    }

    render() {
      return this.props.children;
    }
  }

  ContextProvider.childContextTypes = {};
  Object.keys(context).forEach(key => {
    ContextProvider.childContextTypes[key] = React.PropTypes.any.isRequired; 
  });

  return ContextProvider;
}

And use it as following:

const ContextProvider = createContextProvider(context);

ReactDOM.render(
  <ContextProvider>
    <MyComponent prop1={someVar} />
  </ContextProvider>,
  someDomNode
);
like image 65
Bogdan Savluk Avatar answered Oct 07 '22 15:10

Bogdan Savluk


In React 15 and earlier you can use ReactDOM.unstable_renderSubtreeIntoContainer instead of ReactDOM.render. The first argument is the component who's context you want to propagate (generally this)

In React 16 and later there's the "Portal" API: https://reactjs.org/docs/portals.html

like image 42
tlrobinson Avatar answered Oct 07 '22 16:10

tlrobinson