devtools icon indicating copy to clipboard operation
devtools copied to clipboard

Setup sate don't appear in vue-dev-tool if setup returns render function

Open vutran6853 opened this issue 5 years ago • 10 comments

Version

6.0.0-beta.2

Browser and OS info

Chrome 85

Steps to reproduce

  1. create vue project useing vue cli
  • Pick Typescript, vue 3 preview.
  1. create component HelloWorld.tsx Code
import { defineComponent, reactive } from 'vue'

export default defineComponent({
  name: 'HelloWorld',
  setup() {
    const state = reactive({
      name: 'Foo bar'
    })

    // If I return this only. It will work and appear. But If return my state and jsx, it don't work.
    // return {
    //   state
    // }

    return () => {
      return (
        <div>
          <h1>{state.name}</h1>
        </div>
      )
    }
  }
})

What is expected?

setup should appear and have my state which is from my reactive.

What is actually happening?

setup don't appear in vue-dev-tool in Chrome.


if you return state. setup show up after refresh the page. But I need to return my jsx from my component. is there way to fix this issue? Also if you use .vue and flag lang="tsx" it same problem.

vutran6853 avatar Oct 09 '20 19:10 vutran6853

Any updates?

axelthat avatar Apr 28 '21 08:04 axelthat

import { defineComponent, reactive, h } from 'vue'

export default defineComponent({
  name: 'SetupRender',

  setup () {
    const state = reactive({
      name: 'Foo bar'
    })

    return () => {
      return h('h1', state.name)
    }
  }
})

Akryum avatar May 05 '21 12:05 Akryum

This is tricky because the devtools gets the render function as the setup result.

Akryum avatar May 05 '21 12:05 Akryum

My suggestion

import { defineComponent, ref, h } from 'vue'
import {debug} from '@vue/devtool/debug'

export default defineComponent({
  name: 'SetupRender',

  setup () {
    const name = ref('foo')

   debug({
     name,
   })

    return () => {
      return h('h1', name.value)
    }
  }
})

bichikim avatar Feb 07 '22 15:02 bichikim

My PR for this -> https://github.com/vuejs/core/pull/5383

bichikim avatar Feb 09 '22 05:02 bichikim

I had the same problem

Mrcxt avatar Mar 09 '22 07:03 Mrcxt

I had the same problem

Same. As a workaround, I've copied the getCurrentInstance function from Vuetify: https://github.com/vuetifyjs/vuetify/blob/next/packages/vuetify/src/util/useRender.ts#L7

You use it like so: https://github.com/vuetifyjs/vuetify/blob/next/packages/vuetify/src/components/VApp/VApp.tsx#L25

setup (props, { slots }) {
    const theme = provideTheme(props)
    const { layoutClasses, layoutStyles, getLayoutItem, items, layoutRef } = createLayout(props)
    const { rtlClasses } = useRtl()

    useRender(() => (
      <div
        ref={ layoutRef }
        class={[
          'v-application',
          theme.themeClasses.value,
          layoutClasses.value,
          rtlClasses.value,
        ]}
        style={ layoutStyles.value }
        data-app="true"
      >
        <div class="v-application__wrap">
          { slots.default?.() }
        </div>
      </div>
    ))

    return {
      getLayoutItem,
      items,
      theme,
    }
}

It works for me

michaelnwani avatar Mar 27 '22 21:03 michaelnwani

I had the same problem

Same. As a workaround, I've copied the getCurrentInstance function from Vuetify: https://github.com/vuetifyjs/vuetify/blob/next/packages/vuetify/src/util/useRender.ts#L7

You use it like so: https://github.com/vuetifyjs/vuetify/blob/next/packages/vuetify/src/components/VApp/VApp.tsx#L25

setup (props, { slots }) {
    const theme = provideTheme(props)
    const { layoutClasses, layoutStyles, getLayoutItem, items, layoutRef } = createLayout(props)
    const { rtlClasses } = useRtl()

    useRender(() => (
      <div
        ref={ layoutRef }
        class={[
          'v-application',
          theme.themeClasses.value,
          layoutClasses.value,
          rtlClasses.value,
        ]}
        style={ layoutStyles.value }
        data-app="true"
      >
        <div class="v-application__wrap">
          { slots.default?.() }
        </div>
      </div>
    ))

    return {
      getLayoutItem,
      items,
      theme,
    }
}

It works for me

Just FYI, I stopped using my hack a while ago as I found it to be too cumbersome.

It would be great if we could get an official fix :)

michaelnwani avatar Aug 20 '22 16:08 michaelnwani

去年就关注了这个问题,今年这个问题有什么进展吗?

TLovers avatar Nov 16 '22 08:11 TLovers

Correct me if I'm wrong, but it looks like this affects any components that use the SFC <script setup> syntax. That seems like a pretty significant limitation. Short of adding more magic to the SFC compiler to somehow extract all the var/const/let bindings from the <script setup> block and stick them in an object somewhere, though, or using the debug function noted above, I'm not sure how this could be solved.

blm768 avatar Mar 30 '23 19:03 blm768