Topic: MDBStorage with check throws error

drimaco free asked 1 year ago


Expected behavior Check "is-user-confirmed" in 0.5 time to check it later with a if statement.

Actual behavior "Uncaught TypeError: Cannot read properties of undefined (reading 'is-user-confirmed')" in console every 3 seconds - globally in project. Callback also isnt logged. Dosnt matter if is-user-confirmed exists or not.

Resources (screenshots, code snippets etc.)

const [isUserConfirmed] = useMDBStorage("is-user-confirmed", false, {
    time: 0.5,
    callback: () => {
    console.log("callback"); }, 
});

Krzysztof Wilk staff answered 1 year ago


Hi!

This error is shown because there's no expiration time for that storage item. You have to use it like that:

const [isUserConfirmed, { setItem: setIsUserConfirmed }] = useMDBStorage("is-user-confirmed", false, {
    time: 0.5,
    callback: () => {
    console.log("callback"); }, 
});

and somewhere in the code, i.e.:

<MDBBtn onClick={() => setIsUserConfirmed(true, 1)}>Click</MDBBtn>

Now the expiration time is set in the dedicated "expiryList" storage element. It will check this item every half of the second (according to your code) and the callback will fire when this element is expired :)

Keep coding!



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • User: Free
  • Premium support: No
  • Technology: MDB React
  • MDB Version: MDB5 1.4.0
  • Device: PC
  • Browser: Chrome
  • OS: Chrome 95.0.4638.69 Win11
  • Provided sample code: No
  • Provided link: No