Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: useHead accepts more types of argument #6

Merged
merged 8 commits into from Jan 19, 2021
Merged

Conversation

antfu
Copy link
Member

@antfu antfu commented Jan 19, 2021

This makes useHead more flexible, resolves #4

Plain

useHead({
	title: 'FooBar'
})

Function

useHead(() => ({
	title: 'FooBar'
}))

Ref / Computed Ref

const headObject = ref({
	title: 'FooBar'
})

useHead(headObject)

headObject.title = 'BarFoo'
const headObject = computed(() => {
	title: 'FooBar'
})

useHead(headObject)

@egoist
Copy link
Collaborator

egoist commented Jan 19, 2021

Maybe we should disallow using plain object, since sth like useHead({ title: `clicked ${count.value} time` }) won't be reactive.

@antfu
Copy link
Member Author

antfu commented Jan 19, 2021

A reactive object could also act as a plain object on types.

const obj = reactive({ title: 'foo' })
useHead(obj)

And since we passed it to ref, usages like this is also be unwrapped automatically:

const title = computed(() => `clicked ${count.value} time`)
useHead({ title })

The case you are talking about is the caveat of Vue's reactivity system itself which developers using Composition API should already aware of. I'd think it's better to have a more concise and flexible API for users to choose from (in some cases there could be all static data as well). What do you think?

@egoist
Copy link
Collaborator

egoist commented Jan 19, 2021

That makes sense 👌

@egoist
Copy link
Collaborator

egoist commented Jan 19, 2021

But #4 wants the value of title to be a ref, which isn't what this PR resolved.

@egoist
Copy link
Collaborator

egoist commented Jan 19, 2021

Looks good to me otherwise, README also needs to updated to reflect changes.

@antfu
Copy link
Member Author

antfu commented Jan 19, 2021

It should be unwrapped automatically with ref

import { ref } from 'vue'

const title = ref('hi')
const object = ref({ title })
console.log(JSON.stringify(object.value)) // {"title":"hi"}
console.log(object.value.title) // hi

@egoist
Copy link
Collaborator

egoist commented Jan 19, 2021

👌

It should be unwrapped automatically with ref

import { ref } from 'vue'

const title = ref('hi')
const object = ref({ title })
console.log(JSON.stringify(object.value)) // {"title":"hi"}
console.log(object.value.title) // hi

@egoist egoist mentioned this pull request Jan 19, 2021
@antfu
Copy link
Member Author

antfu commented Jan 19, 2021

README updated.

@egoist
Copy link
Collaborator

egoist commented Jan 19, 2021

Types also need a refresh
image

@antfu
Copy link
Member Author

antfu commented Jan 19, 2021

Updated with the last commit, guess it should be good now

@egoist egoist merged commit 76cdda7 into main Jan 19, 2021
@egoist egoist deleted the feat/smart-arg branch January 19, 2021 10:46
@egoist
Copy link
Collaborator

egoist commented Jan 19, 2021

@shipjs prepare

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Allow refs
2 participants