reactjs - Display SVG string in the React frontend - Stack Overflow
I have encountered an issue while displaying a scraped SVG string on the frontend in a React application. Here's a brief overview of the process:
I used Node.js and Puppeteer to scrape a price chart (in SVG format) from a website (as shown in the image below)
I created an API to serve the scraped SVG data and successfully fetch it on the frontend using fetch.
On the React frontend, I attempted to render the SVG using
dangerouslySetInnerHTML
. However, the API's response is a long string, and I am unsure how to correctly handle and display it.
If anyone with experience in working with SVGs, Puppeteer, or similar scenarios could provide guidance or suggest best practices for rendering the scraped SVG in React, I would greatly appreciate it.
Here's the code I developed:
const SvgChart: React.FC = () => {
const [svg, setSvg] = useState<string | null>(null);
useEffect(() => {
const fetchSvg = async () => {
try {
const response = await fetch('http://localhost:3000/api/svg-chart');
const data = await response.json();
setSvg(data.svg);
} catch (error) {
console.error('Error fetching SVG:', error);
}
};
fetchSvg();
}, []);
return (
<div>
{svg ? (
<div dangerouslySetInnerHTML={{ __html: svg }} />
) : (
<p>Loading chart...</p>
)}
</div>
);
};
export default SvgChart;
- 都想取代PC 这些产品究竟还欠缺什么?
- 移动互联网迅猛发展连接人与服务成为重要趋势
- 苹果允许预装软件可卸载 或因遭受安卓手机阵型冲击
- 安卓L最新曝光:可发挥64位计算特性!
- 传言成真:微软宣布以72亿美元收购诺基亚手机部门
- java - Custome Recipe Parsing Error when developing mod with fabric in Minecraft 1.21.4 - Stack Overflow
- Vercel hosted SvelteKit app keeps 404ing on form submit? - Stack Overflow
- javascript - Is there a way to get the difference between 2 points by click and hold with the mouse on a line on chart.js? - Sta
- Laravel Livewire Pagination is not responsive - Stack Overflow
- mvvm - How to Call ViewModelProvider without ref in Flutter - Stack Overflow
- python - Unable to Fetch Data from PostgreSQL on Databricks - Connection Attempt Failed - Stack Overflow
- Assigning Latitude and Longitude to Turtles in NetLogo from a GIS Map - Stack Overflow
- c# - While Downloading the Excel file from a location it is not able to download, showing check internet connection - Stack Over
- smartcontracts - FailedCall() with OpenZeppelin meta transactions (ERC2771Forwarder and ERC2771Context) - Stack Overflow
- solrcloud - How to use "or" in an eDisMax query in Solr 9.4? - Stack Overflow
- netcdf4 - Issue with renaming netCDF dimensions with NCO's ncrename - Stack Overflow
- command prompt - byobu - how to disable byobu_prompt_runtime - Stack Overflow