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

350
Views
How to setState base64 string as image in React?

I received a base64 string as image from the server and I need to set src of html img tag. When I set the state of img in useEffect hook it doesn't show in browser.

In Chrome browser it gives me net::ERR_INVALID_URL error in the javascript console.

import React, { useState, useEffect } from "react";

function Captcha(props) {
  const [captchaImg, setCaptchaImg] = useState("");
  useEffect(() => {
     setCaptchaImg("iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==");
  }, []);

  return (
   <div>
      <img
       alt="captcha"
       id="dnt-captcha-img"
       src={`data:image/png;base64,${captchaImg}`}
      />
   </div>

  );
}

export default Captcha;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to specify the correct Content-type, Content-encoding and charset (eg. data:image/jpeg;charset=utf-8;base64):

/*depends on what you want to pass, might not be needed here*/
setCaptchaImg("data:image/jpeg;charset=utf-8;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==");
return <div>
  <img
     alt="captcha"
     id="dnt-captcha-img"
     src=data:image/jpeg;charset=utf-8;base64, {captchaImg}
  />
</div>
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!