I’m streaming my response from by backend through an API call. The response works perfectly but is formatted improperly on the frontend. Paragraphs are not double spaced and just show as another sentence so the response comes back as a big chunk of text.
How do I format the frontend properly to show the streamed text?
Frontend code:
const [summary, setSummary] = useState(‘’);
useEffect(() => {
if (!open) return;
let evtSource;
async function fetchSummary() {
try {
const url = new URL('https://api.url');
url.searchParams.set('router', router);
evtSource = new EventSource(url);
evtSource.onmessage = (event) => {
setSummary((prevSummary) => prevSummary + event.data);
setIsResponseComplete(true);
};
evtSource.onerror = (error) => {
console.error("EventSource failed:", error);
evtSource.close();
setIsResponseComplete(false);
};
} catch (error) {
console.error("Error fetching summary:", error);
setIsResponseComplete(false);
}
}
fetchSummary();
return () => {
if (evtSource) evtSource.close();
};
}, [entity, category, data, open]);
useEffect(() => {
if (open) {
setReferenceImages(getRandomReferences());
}
}, [open]);
And then the text is simply added with the {summary} variable in the react component. Feel like it’s a simple solution but unsure how to properly format without using innerHTML.