Reputation: 23
why "addTodo" in the "handleAdd" method gives me "method expression is not of function type" ?
import React, {useState} from 'react';
import useAddTodo from "../../hooks/todos/useAddTodo";
function Home() {
const [value, setValue] = useState('');
const addTodo = useAddTodo();
const handleChange = e => {
setValue(e.target.value)
};
const handleAdd = e => {
e.preventDefault();
const todo = {
title: value,
status: false,
};
addTodo({
variables: todo,
})
};
return (
<form onSubmit={handleAdd}>
<input onChange={handleChange} value={value} type="text" placeholder="todo title"/>
<input onClick={handleAdd} type="submit" value="add"/>
</form>
);
}
export default Home;
here is my hook
import {useMutation} from 'react-apollo-hooks'
import AddTodoMutation from "./graphql/mutations/AddTodoMutation";
function useAddTodo() {
return useMutation(AddTodoMutation);
}
export default useAddTodo
and my mutation
import gql from 'graphql-tag'
export const AddTodoMutation = gql`
mutation AddTodoMutation($title: String! $status: Boolean!) {
addTodo(title: $title status: $status) {
_id
title
status
}
}
`
export default AddTodoMutation
Can you guys explain to me what seems to be the problem? i would be appreciated!
Upvotes: 2
Views: 1383
Reputation: 5650
According to the documentation, useMutation
returns an array (not a single value).
For example:
const [addTodo, { data }] = useMutation(ADD_TODO);
However, it appears you're returning and invoking the entire array value.
To continue using your custom hook the way it is, try updating it as follows:
import {useMutation} from 'react-apollo-hooks'
import AddTodoMutation from "./graphql/mutations/AddTodoMutation";
function useAddTodo() {
// Destructure the addTodo mutation
const [addTodo] = useMutation(AddTodoMutation);
// Return the value to continue using it as is in the other files
return addTodo;
}
export default useAddTodo
Upvotes: 4