histoire icon indicating copy to clipboard operation
histoire copied to clipboard

[Nuxt] useState crashes histoire - nuxt is undefined

Open Rigo-m opened this issue 3 years ago • 14 comments
trafficstars

Describe the bug

When using useState, histoire crashes

image

Reproduction

https://stackblitz.com/edit/nuxt-starter-szzksv?file=package.json,components%2FHello.vue,components%2FHello.story.vue

System Info

Stackblitz

Used Package Manager

npm

Validations

Rigo-m avatar Jul 22 '22 16:07 Rigo-m

We probably need to mock this nuxt object? @danielroe

Akryum avatar Aug 02 '22 22:08 Akryum

This is a quick mock for useState that I've internally used in some storybook projects:

import { computed, ref, reactive } from "vue"

const globalState = reactive({})

export const useState = (key, initial) => {
  if (!globalState[key]) {
    globalState[key] = initial()
  }

  const computedState = computed({
    get: () => {
      // force reactivity
      return globalState[key]
    },
    set: (value) => {
      globalState[key] = value
    },
  })

  return computedState
}

Although, the problem is broader, because everything that uses some nuxt runtime (useNuxtApp alike) crashes histoire.

Rigo-m avatar Aug 03 '22 07:08 Rigo-m

@Rigo-m I am also having this issue. Could you help me understand how to use the useState mock you provided?

agritheory avatar Aug 11 '22 18:08 agritheory

@agritheory you can put this mock useState file somewhere in your project and auto-import it by adding this vite plugin to histoire vite configuration

Rigo-m avatar Aug 12 '22 08:08 Rigo-m

Hi @Akryum are there any plans to fix this issue or do you have any instructions for a workaround or something like that? Would be very appreciated. Thx.

alxshell avatar Sep 09 '22 10:09 alxshell

Histoire using Nuxt related stuff is also an issue in #300

Lyokolux avatar Oct 18 '22 07:10 Lyokolux

@danielroe @Akryum is there any thoughts/progress on this?

Rigo-m avatar Oct 21 '22 15:10 Rigo-m

Any update on this issue/bug?

Archetipo95 avatar Oct 28 '22 09:10 Archetipo95

i also have the same problem. Any update?

LuciaFe avatar Nov 08 '22 08:11 LuciaFe

@danielroe is working on stuff to simplify the nuxt integration https://twitter.com/danielcroe/status/1590490011501142017

Akryum avatar Nov 10 '22 14:11 Akryum