-
Notifications
You must be signed in to change notification settings - Fork 344
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
Declaring JSX / TSX elements in setup
, instead of render(), loses reactivity
#172
Comments
I believe the setup function is run only once? (AFIK it replaces |
Would that mean I'm just returning those elements as static templates? EDIT Well, that might explain why if I declare them as arrow functions, then call them inside the return, reactivity works. export default createComponent({
name: 'App',
setup () {
// declare <header> and <clicker> elements as
// arrow functions, execute them in the return
const count = ref(0);
const header = () => (
<h1>
Count has been updated { count.value } times
</h1>
);
const clicker = () => (
<button
on-click={
() => count.value++
}
>
Clicker
</button>
);
return () => {
// execute header() and clicker() here to
// retain reactivity
return (
<div id="app">
{ header() }
{ clicker() }
</div>
);
}
},
}); |
Yes, that's exactly the reason, I think this is expected behavior. |
Alright. I'm going to leave this open for a day in case someone wants to add to the discussion. Thanks for the help @dvic, at least now I know why it happens and can work around it. |
Using
JSX
orTSX
we can modularize a component by declaring (somewhat) functional elements in variables. In Vue 2.x this is also possible, but every declared element has to go inside therender
function and reactive variables are handled indata
orcomputed
, kind of breaking the flow of what I'm trying to accomplish.Since with this API we kind of declare everything inside
setup
, I was wondering whether it'd be possible to also declare these modular elements there too, instead of in the returnedrender
function.I can do something like this and the component renders just fine, but
count
reactivity is lost:Is there anything I can do here to accomplish this?
Don't know if what I'm asking horribly defiles any good practices standards out there, though...
Maybe somewhat related issues: #59, #64
UPDATE
I should have mentioned, just in case. I used @liximomo's vue-composition-api-tsx-example as the sandbox to run the code I've written above.
That repo installs babel-preset-vca-jsx, which automatically imports
createElement
ash
and compiles the return ofsetup
from JSX to a valid createElement object.The text was updated successfully, but these errors were encountered: