You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you can see, this function uses a double shabang (!!) operator to test whether a value is defined or not. However, one should not rely solely on type coercion here—it will mistake zero and empty strings for undefined values.
Context
Unless this behavior is intended—in which case it should be expressly stated in the documentation—this function should be re-worked. I came across this issue when I tried to count Snackbars as they constructed.
/*SIMPLIFIED*/const[count,setCount]=useState(0);const{ enqueueSnackbar, closeSnackbar }=useSnackbar();constmyCallback=()=>{enqueueSnackbar('I love snacks',{persist: true,key: count,action: ()=>(<ButtononClick={()=>closeSnackbar(count)}>X</Button>)});setCount(count+1)};
As you can see, when attempting to set zero as a key, the program crashes and outputs the following stack trace:
SnackbarProvider.tsx:181 Uncaught Error: handleEnteredSnack Cannot be called with undefined key
at SnackbarProvider._this.handleEnteredSnack (SnackbarProvider.tsx:181:1)
at chainedFunction (createChainedFunction.js:21:1)
at chainedFunction (createChainedFunction.js:21:1)
at chainedFunction (createChainedFunction.js:21:1)
at chainedFunction (createChainedFunction.js:20:1)
at Object.onEntered (Slide.js:131:1)
at Transition.js:281:1
at Transition.nextCallback (Transition.js:343:1)
at callCallback (react-dom.development.js:13923:1)
at commitUpdateQueue (react-dom.development.js:13944:1)
Why it matters
Zero as an index/key makes sense to most developers. The obscurity of this stack trace doesn't help resolve any confusion that might arise from this, either, which could lead to many minutes of puzzled head-scratching.
The text was updated successfully, but these errors were encountered:
I don't think isDefined is the issue since it explicitly checks for value === 0 and counts 0 as a valid value. The intention has been to support 0 as snackbar key (see #187). I think this line has causes a regression:
Function in question:
notistack/src/utils/constants.ts
Line 84 in d9119b3
Current Behavior
As you can see, this function uses a double shabang (!!) operator to test whether a value is defined or not. However, one should not rely solely on type coercion here—it will mistake zero and empty strings for undefined values.
Context
Unless this behavior is intended—in which case it should be expressly stated in the documentation—this function should be re-worked. I came across this issue when I tried to count Snackbars as they constructed.
As you can see, when attempting to set zero as a key, the program crashes and outputs the following stack trace:
Why it matters
Zero as an index/key makes sense to most developers. The obscurity of this stack trace doesn't help resolve any confusion that might arise from this, either, which could lead to many minutes of puzzled head-scratching.
The text was updated successfully, but these errors were encountered: