score:1
your effect is triggered based on the "shopusers" prop, which itself triggers a redux action that updates the "shopusers" prop and thats why it keeps infinitely firing.
i think what you want to optimize is the rendering of your component itself, since you're already using redux, i'm assuming your props/state are immutable, so you can use react.memo
to re-render your component only when one of its props change.
also you should define your state/props variable outside of your hooks since they're used in the scope of the entire function like so.
in your case, if you pass an empty array as a second param to memo, then it will only fire on componentdidmount, if you pass null/undefined or dont pass anything, it will be fired on componentdidmount + componentdidupdate, if you want to optimise it that even when props change/component updates the hook doesn't fire unless a specific variable changes then you can add some variable as your second argument
react.memo(function(props){
const [isloading, setloading] = usestate(false);
const { getstoreusers, shopusers } = props;
useeffect(() => {
setloading(true);
getstoreusers().then(() => {
setloading(false);
}).catch((err) => {
setloading(false);
});
}, []);
...
})
score:7
you can make a custom hook to do what you want:
in this example, we replace the last element in the array, and see the output in the console.
import react, { usestate, useeffect, useref } from "react";
import reactdom from "react-dom";
import { isequal } from "lodash";
const useprevious = value => {
const ref = useref();
useeffect(() => {
ref.current = value;
});
return ref.current;
};
const app = () => {
const [arr, setarr] = usestate([2, 4, 5]);
const prevarr = useprevious(arr);
useeffect(() => {
if (!isequal(arr, prevarr)) {
console.log(`array changed from ${prevarr} to ${arr}`);
}
}, [prevarr]);
const change = () => {
const temp = [...arr];
temp.pop();
temp.push(6);
setarr(temp);
};
return (
<button onclick={change}>change last array element</button>
)
};
const rootelement = document.getelementbyid("root");
reactdom.render(<app />, rootelement);
live example here.
Source: stackoverflow.com
Related Query
- How to use useEffect hook properly with array dependency. I passed state from redux store and still my component renders infinitely
- How to use useEffect hook with the dependency list as a specific field in an array of objects?
- How to use React Context with useState hook to share state from different components?
- How to delete objects from react state hook array with a button click
- updating state array from inside useEffect with passed props
- How to properly pass an array from a parent React class component state to a child component written with hooks?
- React Hook setting state in useEffect with state in dependency array
- How to use useState hook with array state for children that call the setter function without infinite rendering loop?
- State Update is not reflecting immediately . How to use dependency array for useEffect to show changes?
- How to properly use useField hook from Formik in typescript
- Why is a state variable's setter needed as a dependency with useEffect when passed in through props?
- How to reset React hook state with setTimeout in useEffect
- How to check if state default boolean false is changing to true with React Hook useEffect
- How to properly update an array inside a react hook state
- useEffect Hook - how to detect the change of an object's property in a state array
- How to update React UI from with state in Redux store with useEffect
- How to use material-ui-pickers's date picker with ReactJS's State Hook and pass date to the parent component?
- How do i properly use useEffect for a async fetch call with react? react-hooks/exhaustive-deps
- How can I access a state hook value from a callback passed to a listener?
- In React Hooks. How to set state to a random number and use that to display a string from an array
- Updating state from an array with react useState hook
- How to handle state in useEffect from a prop passed from infinite scroll component
- How to update a function with useEffect (like componentDidUpdate) and use .map function to display from JSON file ReactJS
- How to use useEffect correctly with useContext as a dependency
- React.js: How to properly append multiple items to an array state using its use-state hook setter?
- How to create a static variable from a state variable made with useState hook on ReactJS?
- React infinite-scroll with array from hook state
- How to use SimpleImageSlider in Reactjs with an array of images from firebase firestore
- How to check and return value of state object array and use that to identify which object to take data from
- How do I use startsWith with a form submission when filtering array data from an API in React?
More Query from same tag
- Using .match() in ReactJS - match is not a function?
- CSS arrange SVGs into offset grid
- React-Leaflet Map doesn't update
- Tailwind CSS animations not working in ReactJs/NextJs
- setState in async function
- How to add href to react-bootstrap-table column dynamically?
- How to pass data of a component into another component?{ReactJS}
- Get Type Error: Cannot read property of undefined
- React deploy error = Assertion `args[1]->IsString()'
- ReactJS - Error Message: React.createElement: type is invalid -- expected a string
- fecth id and date from url as pros in react js
- React Router (v3) redirect not redirecting to new location
- Putting value at 100 doesn't fill up SVG circle. What's wrong with my calculation
- Arrow function in render Vs. attribute to carry data
- Why getByRole does not accept the "ul" as a string?
- How to use key generated by firebase database push method in write operation?
- NextJS: Error serializing `.data.data` returned from `getServerSideProps`
- How to Access EditorState from Decorated Component
- Module not found: Can't resolve 'tsparticles' in '/home/manujtiwari/Desktop/portfolio-react/node_modules/react-particles-js/cjs'
- Blank Page when run build
- Passing a string to clickHandler in a react functional component
- jquery escaping quotes to insert element with classes
- How to create file object of pdf from blob url (URL.createObjectURL())
- Unexpected results in testing a stateful React component with hooks?? (Jest)
- How can i update another select tag(sub) from an onchange event of select tag(main) in reactjs?
- React: Do not render menu based on child component state
- Not able to mock a function inside useEffect
- Laravel Auth Integration with React Frontend
- I am not getting home component in react website after npm run build command I am only getting Navbar and Footer
- re render a react component only when the state change