question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useFallback: Reactive nullish coalescing operator

See original GitHub issue

Clear and concise description of the problem

~I want logicalOr, but with the nullish coalescing operator (??).~

I want to write fallback code. As @thedamon pointed out below, nullish coalescing isn’t what we want because we want to be able to short-circuit the fallback with an explicit null.

Context

I’m writing a lot of universal fallback code to check if a user has passed in props.modelValue or props.value so that my component code is backwards compatible.

This ends up with me writing:

const val = computed(() => props.modelValue ?? props.value)

// pass into useVModel(val), etc.

in a lot of components.

Suggested solution

A function called useFallback which would take in args.

Usage

const props = defineProps<{
  someValue: string
  deprecatedValue: string
  defaultValue: string
}>()

const resolvedValue = useFallback(props.someValue,
  props.deprecatedValue,
  props.defaultValue)

Alternative

We could pass in the entire props object and use an Array, like useVModel does.

Additional context

Honestly, I’m just looking for a concise way to declare fallbacks for two values: modelValue and value.

The better solution to my problem is probably to make a feature request to useVModel so that it also handles the emit backwards compatibility.

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:4
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
thedamoncommented, Oct 12, 2022

The better solution to my problem is probably to make a feature request to useVModel so that it also handles the emit backwards compatibility.

Is there appetite to bake some of the ‘vue-bridge’ type functionality into VueUse? then it could maybe be as direct as useBridgedVModel which would be a separate as necessary but potentially reusing the vueUse function bridgified version of useVModel ?

1reaction
JessicaSachscommented, Oct 7, 2022

That’s a good point. In which case, useFallback isn’t really gonna use the nullish coalescing operator, it will let you short-circuit with an explicit null.

We should open another feature request for logicalNullish or nullish and that’ll go in the @vueuse/math package.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nullish coalescing with React and TypeScript | Building SPAs
Notice the nullish coalescing operator ( ?? ). This renders the right-hand operand if the left-hand operand is null or undefined . So,...
Read more >
JavaScript tips for React Developers - Prateek Surana
The Nullish Coalescing operator ( ?? ) returns the right hand side operand when the left hand side is null or undefined and...
Read more >
How to use Optional Chaining and Nullish Coalescing with ...
You can see here in this example with the nullish coalescing operator since the count isn't undefined or null it returns the left-hand...
Read more >
Learn useCallback React Hook in 10 Minutes - YouTube
In this video we are going learn everything about React Hook useCallback. How we can use useCallback hook to memorize the callback function ......
Read more >
Difference between useEffect, useMemo and useCallback in ...
It's a function that prevents your React Hook components from rendering when the props don't change. It returns a memoized value after taking...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found