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

139
Views
extract plain text from react object

somebody knows how could I extract plain text from a react component?

example:

const reactObject = 
  <div className="text-primary">
    <span>this is a react object, </span>
    <b>builded with jsx!!!!</b>
  </div>;
.....
//what should I do inside extractText????
const plainText = extractText(reactObject);
alert(reactObject);//result: "[Object object]"
alert(plainText);// must result: "this is a react object builded with jsx!!!!"
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I really don't know why you need this, but like you were told you in the comments, you can use the useRef for your component, and if you know very well the component structure you can get its children and iterate to get the innerHTML. If you need a generic solution, you should provide some more information.

Here is a stackblitz code solving your specific problem (you can open the console to see the log):

https://stackblitz.com/edit/react-ts-n47abv?file=index.tsx

about 4 years ago · Juan Pablo Isaza Report

0

I have write this recursive function if someone needs

  extractString(obj) {
    if (typeof obj === 'string') return obj;
    else if (React.isValidElement(obj)) {
      return this.extractString(obj.props.children);
    } else if (Array.isArray(obj)) {
      return obj.map(e => this.extractString(e)).join(' ');
    } else return obj.toString();
  }

I'm using this for show error message at bottom of an input:

<input ref={.....} value={....} ..... />
<p>{this.props.errorMessage}</p>

BUUUUUT if the user still click on the submit button... I want to show the same text in the default browser error message without rewrite setting the same massage only once.

const errorMessage = this.extractString(this.props.errorMessage);
//this is the ref to the input
this.input.current.setCustomValidity(errorMessage);
about 4 years ago · Juan Pablo Isaza Report
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!