vue-redux-hooks-copy
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

vue-redux-hooks CircleCI codecov

Install

npm i redux vue-redux-hooks
yarn add redux vue-redux-hooks

API

ReduxStore

// store.ts
import { createStore, AnyAction } from 'redux'

function todos(state: string[] = [], action: AnyAction) {
  switch (action.type) {
    case 'ADD_TODO':
      return state.concat([action.text])
    default:
      return state
  }
}

export const store = createStore(todos, ['Use Redux'])

// after augmenting ComponentCustomProperties all other typings are not required
declare module 'vue' {
  interface ComponentCustomProperties {
    $redux: typeof store
    $reduxState: ReturnType<typeof store.getState>
  }
}
// main.ts
import { createApp } from 'vue'
import { install } from 'vue-redux-hooks'
import { store } from './store'

createApp(App).use(install(store)).mount('#app')

Options

mapState

// App.vue
import { mapState } from 'vue-redux-hooks'

export default {
  data() {
    return {
      search: 'Today',
    }
  },
  computed: {
    ...mapState({
      filteredTodos(this: { search: string }, todos: State) {
        return todos.filter((todo) => todo.includes(this.search))
      },
    }),
    // or
    filteredTodos() {
      return this.$reduxState.filter((todo) => todo.includes(this.search))
    },
  },
}

mapDispatch

// App.vue
import { mapDispatch } from 'vue-redux-hooks'

export default {
  methods: {
    ...mapDispatch({
      addTodo: (dispatch: Dispatch, text: string) =>
        dispatch({
          type: 'ADD_TODO',
          text,
        }),
    }),
    // or
    addTodo(text: string) {
      return this.$store.dispatch({
        type: 'ADD_TODO',
        text,
      })
    },
  },
}

Hooks

useStore

// App.vue
import { useStore } from 'vue-redux-hooks'

export default {
  setup() {
    const store = useStore<Store>()

    const initialState = store.getState()

    return { initialState }
  },
}

useState

// App.vue
import { useState } from 'vue-redux-hooks'
import { computed, ref } from 'vue'

export default {
  setup() {
    const search = ref('Today')

    const todos = useState<State>()

    const filteredTodos = computed(() =>
      todos.value.filter((todo) => todo.includes(search.value)),
    )

    return { filteredTodos }
  },
}

useSelector

// App.vue
import { useSelector } from 'vue-redux-hooks'

export default {
  setup() {
    const search = ref('Today')

    const filteredTodos = useSelector((todos: State) =>
      todos.filter((todo) => todo.includes(search.value)),
    )

    return { filteredTodos }
  },
}

useDispatch

// App.vue
import { useDispatch } from 'vue-redux-hooks'

export default {
  setup() {
    const dispatch = useDispatch<Dispatch>()

    const addTodo = (text: string) =>
      dispatch({
        type: 'ADD_TODO',
        text,
      })

    return { addTodo }
  },
}

RTK Query

createApi

// pokemonApi.ts

// Need to use the Vue-specific entry point to allow generating Vue hooks
import { createApi } from 'vue-redux-hooks'
import { fetchBaseQuery } from '@reduxjs/toolkit/query'
import type { Pokemon } from './types'

// Define a service using a base URL and expected endpoints
export const pokemonApi = createApi({
  reducerPath: 'pokemonApi',
  baseQuery: fetchBaseQuery({ baseUrl: 'https://pokeapi.co/api/v2/' }),
  endpoints: (builder) => ({
    getPokemonByName: builder.query<Pokemon, string>({
      query: (name) => `pokemon/${name}`,
    }),
  }),
})

// Export hooks for usage in function components, which are
// auto-generated based on the defined endpoints
export const { useGetPokemonByNameQuery } = pokemonApi
// App.vue
import { toRefs, ref } from 'vue'
import { useGetPokemonByNameQuery } from './pokemonApi'

export default {
  setup() {
    const name = ref('Pikachu')

    const skip = ref(false)

    const query = useGetPokemonByNameQuery(name, {
      refetchOnReconnect: true,
      skip,
    })

    return { name, skip, ...toRefs(query) }
  },
}

Package Sidebar

Install

npm i vue-redux-hooks-copy

Weekly Downloads

1

Version

0.1.0

License

MIT

Unpacked Size

162 kB

Total Files

6

Last publish

Collaborators

  • wobsoriano