0

Im wondering, how is this code giving object is possibly undefined error

  if (newFocus) {
    if (viewCache[viewId] !== undefined) {
      dispatch(ViewActions.focusOn(viewCache[viewId].focus));
    } else {
      dispatch(ViewActions.focusOn(newFocus));
    }
  }

and line 3 is giving me error, viewCache[viewId] is possibly undefined even when wrapped in if (viewCache[viewId] !== undefined)

1 Answer 1

1

The error seems to be pointing out that viewCache can be undefined. You can add a check for its existence too

if (newFocus) {
    if (viewCache && viewCache[viewId] !== undefined) {
      dispatch(ViewActions.focusOn(viewCache[viewId].focus));
    } else {
      dispatch(ViewActions.focusOn(newFocus));
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.