Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Return Object from ReactDOM.render

I have a JS file that exposes a function as shown below (following is sample code):

import ReactDOM from 'react-dom';
import React from 'react';
import Component from './Component';
(function() {
  if (!window || !window.someFunction) {
    window.someFunction = (selector) => {
      ReactDOM.render(<Component />, document.querySelector(selector));
      return objectFromComponent; // I don't know how to do this
    }
  }
})();

When this function is called the component is rendered to the DOM. The Component actually generates a particular object that I would like to be returned somehow to this piece of JS code. I tried the following:

  1. Storing in localStorage in Component.jsx and reading from callback in : ReactDOM.render(<Component />, document.querySelector(selector), () => {//listening for storage event});
  2. window.parent.postMessage in Component.jsx and event listener for "message" event in the function shown above.

Is there a more elegant way to do it with React itself rather than complicating the logic with localStorage and event listeners?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!