Answer the question
In order to leave comments, you need to log in
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)) //допустим тут я хочу задиспатчить данные
}
Answer the question
In order to leave comments, you need to log in
As Lynn "Coffeeman" already said : in order for typescript not to swear at null
you, 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) {
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question