I think what @TvG told is right on spot. Object comparison are done via references.
When you are creating the object the way you have done in the code will create new reference object everytime.
const role = useRole({ data: {} });
Even if you do it like this:
let defaultRole = { data: {} }
const role = useRole(defaultRole);
It will be creating the new object every time . the value of defaultRole will be recalculated in every render.
What can be done here is , React provides us useRef method which will not change on rerenders unless changed explicitly. Here is the link for you to read:
useRef docs
You can do something like this:
const { useEffect, useState, useRef } = React
function App() {
const [test, setTest] = useState(true);
console.log("running this")
let baseObj = {
data: {}
}
const roleDefaultValueRef = useRef(baseObj)
const role = useRole(roleDefaultValueRef.current);
useEffect(() => {
setTest(false);
}, []);
return 1
}
function useRole({ data }) {
const [role, roleSet] = useState(false);
useEffect(() => {
console.log("looping");
roleSet({});
debugger
}, [data]);
return role;
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script>
<div id="root"></div>
Folllow up question why object like data = {} will work in this case.
SO your useRole has an effect which is dependent on data. Let say you called useRole as useRole({})
now in your useRole you are spreading data value out of this passed object {}
so this line
export function useRole({ data }) {
// data will be evaluated as undefined and undefined will remain same on //consecutive rerenders and hence the effect will not run
}
This is the reason it is running when you are passing a blank object to useRole.
Hope this helps.
FOr understanding try to print the value of data in useRole, you will definitely understand it :)
1 === 1 //true
undefined === undefined //true
{} === {} // false