admin管理员组文章数量:1417677
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
1 Answer
Reset to default 6You 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)
}
本文标签:
版权声明:本文标题:javascript - React Hook is called in function "onSubmit" which is neither a React function component or a cust 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745265252a2650559.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论