B
B
broke2022-02-08 21:16:01
typescript
broke, 2022-02-08 21:16:01

How to check the existence of an object in dispatch?

Good evening! Tell me how can I check whether an object exists in the dispatcher or not. For example, I have a type that has

interface DataState {
  Data: ExpectedData| null;
   Favorite: false;
}
const data = useSelector(state => state.DataState.Data)
const favorite = useSelector(state => state.DataState.Favorite)
const dispatch = useDispatch()
.......

if(favorite){
   dispatch(someFunction(data))  //допустим тут я хочу задиспатчить данные
}

How to make the dispatcher not swear that the argument = null ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2022-02-09
@Aetae

As Lynn "Coffeeman" already said : in order for typescript not to swear at nullyou, you must make sure that there is no null. Pretty obvious, yes.
In practice, you need to do either:

const data = useSelector(state => state.DataState.Data) ?? defaultData;
Or like this:
if (favorite && data) {
Well, or something else, to your taste.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question