I have a simple React function component and noticed some strange behavior while trying to use hooks. It persists even after commenting out all content of the function so I don't think it has anything to do with changing state or props.
this code:
let renderNum = 0;
export default function MyComponent(props: any) {
console.dir(props)
renderNum += 1;
console.log('renderNum ' + renderNum)
return (
<>
<h1>renderNum {renderNum}</h1>
</>
)
};
results in the following behavior: prtScr
- the counter starts at 0
- there's a console.dir(props)
- the counter is incremented by 1
- there's another console.dir(props) <--- 2nd reder!?
- the view displays renderNum 2
How is this possible?
Why is the counter incremented twice?
And even so - why is there no second log of the counter?
How can the console.dir() be executed twice but not console.log()?
notes:
- the props never change (empty list)
- tried disabling all other code, including all other components in parent
- removed all uses of context or redux stuff
- tried alternative ordering of logging and incrementing
- I'm a newbie with React and might miss some basic fact
- this is relevant to me because I'm trying to add dynamic RTK listeners and need to add it inside this component; otherwise cannot use hooks
finally:
If this is expected behavior (), how else can I add a listener to a redux-toolkit action that does not alter any store value? I'd post this a separate question but not sure how to formulate it properly.