score:0
Accepted answer
maybe you can try put allques
state on the array dependencies of second useeffect parameter, because it make watch the state which is allques, then when allques
changed so component do side effect on first parameter of useeffect()
useeffect(() => {
fetch('http://localhost:4000/questions')
.then(res => {
return res.json();
})
.then(data => {
if (sortway === 'latest') {
data.sort((a, b) => b.sortorder - a.sortorder);
}
setallques(data);
});
}, [allques]);
score:2
don't directly sort sortarr1, first of all, make its copy and then sort that copy and then set sorted array into the state.
you can copy array as below:
let copiedarr = [...sortarr1];
copiedarr.sort(sortfn);
setsortarr1(copiedarr);
Source: stackoverflow.com
Related Query
- react component state change after ajax call but does not rerender component
- Why does react component do not re render after changing state
- React Redux-a child component does not re-render on state change
- Component does not render based on state change in react-redux
- React component does not update with the change in redux state
- React function component parent state change does not re-render child component
- State variable of react component does not change after fetch
- React functional component state change does not trigger child component to re-read its props
- React Component Does not Always Rerender After State Change
- React Child Component Not Updating After Parent State Change
- React component not re-rendering on state change
- React-redux component does not rerender on store state change
- React shouldComponentUpdate() is called even when props or state for that component did not change
- React leaflet center attribute does not change when the center state changes
- React SVG import as a Component does not render
- React Dev Tools does not show Component Names or State Variable Names
- react is not updating functional component state on input change
- React js changing state does not update component
- React child component not re-rendering on parent state change
- Component does not rerender but redux state has changed via react hooks
- React - will not re render on state change
- React useCallback does not get updated on state change
- React router dom redirect problem. Changes url, does not render component
- React Router does not render Component
- React Component not updating with state change
- Change in redux state does not cause change in component / componentDidUpdate not called
- React URL changes but component does not render
- React child component do not re-render on it state change
- React child component not re-rendering on state change
- react stateless child components do not update on state change in parent component
More Query from same tag
- Send object and list of image to server in Reactjs
- Redux state - maximum recommended size
- useEffect simulating componentWillUnmount does not return updated state
- How to make a div out of the screen but only slide that element?
- How does one pass a prop as null in react?
- Redux - Global concept - Display message depends on boolean
- Apollo Client: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)
- React 17: Should useEffect / useCallback be called before an element is mounted?
- Material-UI Masonry: Remove space on right side
- React Hook Form with AntD Styling
- Trying to toggle the background color of a table row on hover using React, but nothing happens
- React.js. How to show only first object of fetched API?
- How to access data in child component of query called in parent component in rtk query?
- React equivalent for ng-repeat
- Error building react-create-library with Material UI
- Why wont my create-react-app content center?
- accessing state inside class in react
- Type '{ name: string; }' is not assignable to type 'ReactNode'
- Mutating the DOM in React
- Global way of tracking if user is logged in in React?
- React error: call child method in parent component
- Call React Function not working
- Find out who is rendering a component when debugging React
- How to load virtual dom once the data from api are loaded React
- getting 400 Bad Request when trying to upload to aws s3 bucket
- useEffect do not work in context api in react
- Is there a way to hook ReactJS onto dynamic elements from backbone?
- How to change a <a> tag color when I click on it and it doesn't change until I change my URL
- Mapping array returns undefined values
- disabling mouse events react