Skip to content

TypeScript types error with useMutation custom hook #1060

@avalladaresm

Description

@avalladaresm

Describe the bug
Hello, I'm just getting started in migrating a project I'm building from Redux to React Query 😃.
So I'm trying to replicate the custom hook you did here.

export const useCreateLog = () => {
	return useMutation((values) => {
		console.log(values)
		return axios.post('http://localhost:8080/docs/logs', { data: values })
	}, {	
		onSuccess: (data, variables) => {
			console.log('success, yey', data, variables)
			queryCache.invalidateQueries('Logs')
		},
		onError: () => {
			console.log('what happened?')
		}
	})
}

Then, I'm using the hook in a .tsx file, I'm importing it as import { useCreateLog } from '../../services/logs', then I'm creating the state variables, const [createLog, createLogInfo] = useCreateLog() and then using createLog inside a function the submits the data,

  const onFinish = values => {
		const log = {
			userName: values.username,
                        date: 'something here',
                        type: 'something here',
                        description: 'something here',
                        data: JSON.stringify('something here')
		}
		createLog(log)
  }

However, vscode is complaining about an error on createLog(log) that says Argument of type '{ userName: any; date: moment.Moment; type: LogTypes; description: string; data: string; }' is not assignable to parameter of type 'undefined'.ts(2345), but even so, still does the axios.post operation, I just that to be able to remove that error.

Any tips would be greatly appreciated! 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions