score:0
i think the issue is that sse
doesn't persist for the full lifetime of the component.
to avoid this issue, you should use useref
hook.
sse
will persist for the full lifetime of the component.
const sse = useref<any>();
useeffect(function() {
sse.current = new eventsource('/api/v1/events');
sse.current.onmessage = (ev : messageevent) => {
const item = json.parse(ev.data);
if (item === null) {
sse.currernt.close();
return;
}
console.log('received', item.msg);
setstate((items) => ([...items, item]));
};
return () => {
sse.current.close();
};
}, []);
Source: stackoverflow.com
Related Query
- Real DOM is never updated while the state is mutated
- React state not being updated in Jest while working in the application
- virtual dom and real dom issue while changing state
- Uncaught Error: Invariant Violation: findComponentRoot(..., ...$110): Unable to find element. This probably means the DOM was unexpectedly mutated
- ReactJs: How to pass the initial state while rendering a component?
- react setState callback doesn't have the updated state
- redux getState() doesn't return the updated state
- Why is the DOM not updating with state change in ReactJS?
- How do I sync state from the DOM to my React Component in an Isomorphic Application?
- Javascript: Is the Entire DOM Updated When You Make Changes to the DOM?
- Console.log() after setState() doesn't return the updated state
- React hooks - wait until the state has been updated
- Whole page is flickered and jump to top when a part of the state is updated in react-redux application
- ReactJs - How to get updated state while inside a thunk promise
- What is the better way to update other state variable which is dependent on another state variable while re-rendering?
- useReducer returning a promise instead of the updated state
- What is meant by "Data not originating from the state will never be drafted" in immer
- data is fetched but the state is not updated
- What is the simplest way to pass state while using React Router?
- How to access the updated state in the body of a function that has already called setState?
- React setState componentDidUpdate React limits the number of .. when trying to update the state based on updated state
- Why my state is not getting updated even after writing the setState method?
- useEffect() chain function call not getting the updated redux state
- Why the initial loading state is set to true while using useState?
- Image is not rendering in the DOM while passing as props in the App.js file?
- React Hooks State is not updated the first time
- Working of push in react while using redux to update the state
- How to fix the state update in react while getting data from firestore database
- Material UI Slider component, update state on mouse release, while sliding in real time
- React doesn't re-render the Virtual DOM after the state is changed
More Query from same tag
- How to make scrollbar in lesser width/length compared to the container
- How to trigger a child component event in componentDidMount
- How to initialize random state when using React?
- Don't render component on specific route
- valid way to update a state property in react?
- pass async results from parent to child component react functional component
- Changing state on route change in React
- Attach Authorization header for all axios requests
- Call multiple functions onClick ReactJS
- Dynamically change Next.js style type for element
- Nested route request interpreted as request for CSS file
- react.js text div wont scale with my page but the image div will
- Does declaring a React.memo inside another functional component provide the memoization performance as it is intended?
- How to render a component multiple times in react
- Change the default number of rows in griddle
- How to properly loop through an array to form association map
- How can I print these dynamic data in jsPDF?
- React JS Development Build Showing a White Screen
- 'FontAwesomeIcon' is not defined
- Using methods and rerendering React Components in array
- Updating the user coming from react-with-firebase-auth
- Adding <a> tag to App.js Constructor from Component Props
- Cancel only current axios request in React application
- Spaces not registered when using onChange on <input>
- How Do I Use useReducer with useQuery?
- Making request parameter optional in axios get request
- Why am I getting match is not a function when it is clearly defined?
- Javascript Onclick=window.open() function will open a new page every time I refresh the page?
- State in react component won't render
- Converting 'resize' eventListener from class to functional component?