1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[ReactJS]

Discussão em 'Mobile' iniciado por Stack, Fevereiro 23, 2021.

  1. Stack

    Stack Membro Participativo

    No state da função do arquivo REDUCE.TS, eu estou tentando adicionar total: 0 como no código abaixo.

    Mas ele me mostra o seguinte erro no arquivo STORE.TS. Acho que pode ser do TypeScript, porem não consigo achar o que seria o erro:

    const initialStatePost: ItemCarrinho = {
    carrinho: 0,
    total: 0
    }


    Segue aqui o erro completo:


    No overload matches this call. Overload 1 of 2, '(reducer: Reducer<ItemCarrinho, any>, enhancer?: StoreEnhancer<unknown, unknown> | undefined): Store<ItemCarrinho, any>', gave the following error. Argument of type '(state: ItemCarrinho | undefined, action: any) => ItemCarrinho | { carrinho: number; }' is not assignable to parameter of type 'Reducer<ItemCarrinho, any>'. Type 'ItemCarrinho | { carrinho: number; }' is not assignable to type 'ItemCarrinho'. Property 'totalc' is missing in type '{ carrinho: number; }' but required in type 'ItemCarrinho'. Overload 2 of 2, '(reducer: Reducer<ItemCarrinho, any>, preloadedState?: { carrinho: number;

    - Segue um exemplo mínimo do código :

    REDUCE.TS

    import {ItemCarrinho, TypesCarrinho} from './types'

    const initialStatePost: ItemCarrinho = {
    carrinho: 0
    }

    function reducerCarrinho(state = initialStatePost, action: any) {

    switch(action.type) {
    case TypesCarrinho.ADD_CARRINHO:
    return {
    carrinho: state.carrinho + 1
    }

    case TypesCarrinho.DECREASE_CARRINHO:
    return{
    carrinho: state.carrinho - 1
    }

    default:
    return state
    }
    }

    export default reducerCarrinho


    STORE.TS

    import { createStore } from 'redux'
    import { composeWithDevTools } from 'redux-devtools-extension'

    import reducerCarrinho from './ducks/carrinho/reduce'


    const store = createStore(reducerCarrinho, composeWithDevTools())


    export { store }



    TYPES.TS

    export enum TypesCarrinho {
    ADD_CARRINHO = 'ADD_CARRINHO',
    DECREASE_CARRINHO = 'DECREASE_CARRINHO',
    }

    export interface ItemCarrinho{
    carrinho: number
    }


    ACTION.TS

    import { action } from 'typesafe-actions'
    import { TypesCarrinho } from '../../ducks/carrinho/types'

    export const soma = () => action(TypesCarrinho.ADD_CARRINHO)

    export const subtracao = () => action(TypesCarrinho.DECREASE_CARRINHO)

    Continue reading...

Compartilhe esta Página