<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thomas Fitzgerald</title>
    <description>The latest articles on DEV Community by Thomas Fitzgerald (@phantompooper).</description>
    <link>https://dev.to/phantompooper</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1106645%2Fda46997f-9791-448d-8660-1fe66bb51148.png</url>
      <title>DEV Community: Thomas Fitzgerald</title>
      <link>https://dev.to/phantompooper</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/phantompooper"/>
    <language>en</language>
    <item>
      <title>Vue3 + typescript + vuex4 [Help]</title>
      <dc:creator>Thomas Fitzgerald</dc:creator>
      <pubDate>Thu, 22 Jun 2023 19:57:43 +0000</pubDate>
      <link>https://dev.to/phantompooper/vue3-typescript-vuex4-4j07</link>
      <guid>https://dev.to/phantompooper/vue3-typescript-vuex4-4j07</guid>
      <description>&lt;p&gt;I'm trying to work with vuex4 and vue3 composition api (SFC)  with typescript. I'm not seeing the reactivity and I think i'm not understanding the docs correctly. My issue is the data I'm trying to load from a .json file doesn't initially load (onMounted) when the component is created, and thus I don't have access to any of the data. But on a hot-reload (vite) the data is accessible. Assuming because it was loaded in the state in the previous mounting of the component. Here is my code, Ill try to explain it in depth.&lt;/p&gt;

&lt;p&gt;store/index.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Athlete } from './../Athlete';
import { type InjectionKey } from 'vue'
import { createStore, Store, type ActionContext, useStore as baseUseStore } from 'vuex'
import { api } from '../api';

export interface State {
    athletes: Athlete[]
}

export const key: InjectionKey&amp;lt;Store&amp;lt;State&amp;gt;&amp;gt; = Symbol();

const Mutations = {
    SetAthletes: 'setAthletes'
}

export const Actions = {
    GetAthletes: 'getAthletes'
}

export const Getters = {
    GetAthlete: 'getAthlete'
}

export const athleteStore = createStore({
    state: {
        athletes: [],
    },
    getters: {
        [Getters.GetAthlete]: (state: State): Athlete =&amp;gt; state.athletes[0]
    },
    mutations: {
        [Mutations.SetAthletes]: (state: State, athlete: Athlete) =&amp;gt; {
            state.athletes.push(athlete);
            state.athletes = [...state.athletes];
        }
    },
    actions: {
        [Actions.GetAthletes]: async (context: ActionContext&amp;lt;State, State&amp;gt;) =&amp;gt; {
            const athlete = await api.getAthletes();
            context.commit(Mutations.SetAthletes, athlete);
        }
    }
});
export function useStore () {
    return baseUseStore(key)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;api.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import AthleteData from '@/assets/data.json';

interface IApi {
    getAthletes: () =&amp;gt; Promise&amp;lt;Athlete&amp;gt;;
}

const getAthletes = async (): Promise&amp;lt;Athlete&amp;gt; =&amp;gt; {
    const jsonObj = AthleteData.data[0];
    return messageToAthlete(jsonObj);
}

export const api: IApi = {
    getAthletes,
}

export default api;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;main.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { athleteStore, key } from './modules/Athlete/store'

const app = createApp(App)
  .use(router)
  .use(vuetify);

app.use(athleteStore, key);

app.mount('#app');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and finally my component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script setup lang="ts"&amp;gt;
import { Athlete } from '@/modules/Athlete/Athlete';
import { Getters, useStore } from './store';
import ReportTableVue from '../../components/ReportTable.vue';
import { ref, onMounted, computed } from 'vue';

const store = useStore();
store.dispatch('getAthletes');

// onMounted(() =&amp;gt; {
//   store.dispatch('getAthletes');
// })

// const athlete: Athlete = ref(store.getters[Getters.GetAthlete]).value
//const athlete: Athlete = computed(() =&amp;gt; store.state.athletes[0]).value

//console.log("athlete: ", athlete);

&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see I tried a few things in the component but nothing worked as expected. In the api layer there is a conversion to cast it to the type &lt;code&gt;Athlete()&lt;/code&gt; I just didn't include it because it's a bit of code. When I hard refresh the browser I keep getting &lt;code&gt;undefined&lt;/code&gt;. Checking the dev tools I can see all the data is there in the state, just not accessible. Here are the docs I referred to and tried before posting this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://vuex.vuejs.org/guide/typescript-support.html"&gt;https://vuex.vuejs.org/guide/typescript-support.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/64010072/how-to-use-vuex-mapgetters-with-vue-3-sfc-script-setup-syntax"&gt;https://stackoverflow.com/questions/64010072/how-to-use-vuex-mapgetters-with-vue-3-sfc-script-setup-syntax&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any help is appreciated!&lt;/p&gt;

</description>
      <category>vue</category>
    </item>
  </channel>
</rss>
