javascript - React Hook is called in function "onSubmit" which is neither a React function component or a cust

I have a form, and I wanted to call a hooks function usePostAddList() in the ponent AddList() inside th

I have a form, and I wanted to call a hooks function usePostAddList() in the ponent AddList() inside the function onSubmit(). basically the usePostAddList() is to make a POST request.

Here's the code for the AddList():

AddList.jsx

export default function AddList() {
..
..
const onSubmit = (e) => {
    e.preventDefault()

    const data = [
      {
        komoditas,
        areaKota,
        areaProvinsi,
        price,
        tglParsed,
      },
    ]

    // I called it here and it show an error
    usePostAddList(data)
}
..
..
}

Reducer/index.js

export function usePostAddList(data) {
  const [state, dispatch] = React.useReducer(reducer, initState)

  React.useEffect(() => {
    dispatch({ type: ACTIONS.MAKE_REQUEST })
    store
      .append("Sheet2", data)
      .then((res) =>
        dispatch({
          type: ACTIONS.ADD_LIST,
          payload: res,
        })
      )
      .catch((e) => {
        return dispatch({
          type: ACTIONS.GET_ERRORS,
          payload: e,
        })
      })
  }, [])

  return state
}

I have read many solutions like I must write "use" for the name of the function, capitalize the function AddList, but still got this error:

React Hook "usePostAddList" is called in function "onSubmit" which is neither a React function ponent or a custom React Hook function

but if I called the usePostAddList() like this code below somehow it worked :

AddList.jsx

export default function AddList() {
   const { lists, loading, errors } = usePostAddList("test")

   return (
      ...
   )
}

but it didn't solve my problem

I have a form, and I wanted to call a hooks function usePostAddList() in the ponent AddList() inside the function onSubmit(). basically the usePostAddList() is to make a POST request.

Here's the code for the AddList():

AddList.jsx

export default function AddList() {
..
..
const onSubmit = (e) => {
    e.preventDefault()

    const data = [
      {
        komoditas,
        areaKota,
        areaProvinsi,
        price,
        tglParsed,
      },
    ]

    // I called it here and it show an error
    usePostAddList(data)
}
..
..
}

Reducer/index.js

export function usePostAddList(data) {
  const [state, dispatch] = React.useReducer(reducer, initState)

  React.useEffect(() => {
    dispatch({ type: ACTIONS.MAKE_REQUEST })
    store
      .append("Sheet2", data)
      .then((res) =>
        dispatch({
          type: ACTIONS.ADD_LIST,
          payload: res,
        })
      )
      .catch((e) => {
        return dispatch({
          type: ACTIONS.GET_ERRORS,
          payload: e,
        })
      })
  }, [])

  return state
}

I have read many solutions like I must write "use" for the name of the function, capitalize the function AddList, but still got this error:

React Hook "usePostAddList" is called in function "onSubmit" which is neither a React function ponent or a custom React Hook function

but if I called the usePostAddList() like this code below somehow it worked :

AddList.jsx

export default function AddList() {
   const { lists, loading, errors } = usePostAddList("test")

   return (
      ...
   )
}

but it didn't solve my problem

Share Improve this question edited Jul 26, 2020 at 2:51 panji gemilang asked Jul 26, 2020 at 2:20 panji gemilangpanji gemilang 8092 gold badges13 silver badges31 bronze badges 9
  • Where are your props? – fuzzybear Commented Jul 26, 2020 at 2:28
  • what do you mean by that? – panji gemilang Commented Jul 26, 2020 at 2:31
  • What is the error? One of the rules of hooks is they have to be called at the top level of the ponent, so I suspect that's why usePostAddList() doesn't error in your second example but it does when it's nested inside of onSubmit. – EvanMorrison Commented Jul 26, 2020 at 2:43
  • so what should I do? I can't call useReducer inside a function? – panji gemilang Commented Jul 26, 2020 at 2:52
  • Are you importing React in your AddList file? – ludwiguer Commented Jul 26, 2020 at 3:31
 |  Show 4 more ments

1 Answer 1

Reset to default 6

You can only use hooks inside functional ponents and at the top of your ponent. Check rules of hooks for more information on why.

You can use your custom hook as follows:

export default function AddList() {
 const [data,setData] = useState()
 const { lists, loading, errors } = usePostAddList(data)

 return (
  ...
 )
}

And update your data at onSubmit function:

const onSubmit = (e) => {
 e.preventDefault()

 const data = [
  {
    komoditas,
    areaKota,
    areaProvinsi,
    price,
    tglParsed,
   },
 ]

 // Call set data
 setData(data)
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745265252a4619414.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信