[英]Event handler throws invalid hook call error
I am learning React and trying to implement a simple onClick button in a child Component only to get an "Invalid hook call".我正在学习 React 并尝试在子组件中实现一个简单的 onClick 按钮,只是为了获得“无效的挂钩调用”。 I've gutted a lot of my program to isolate just the problem.
我已经删除了很多程序来隔离问题。
Parent Component:父组件:
import React, { useState } from "react";
import Menubar from '../Menubar/Menubar'
function SortingVisualizer() {
const [arr, setArr] = useState([]);
function newArray() {
const tempArr = [];
for(let i = 0; i < 100; i++) {
tempArr.push(Math.floor(Math.random() * 100) + 5)
}
setArr(tempArr);
}
return (
<div id="main-container">
<Menubar
data={arr}
/>
</div>
)
}
export default SortingVisualizer;
Child Component in another module:另一个模块中的子组件:
import React from "react";
import newArray from '../SortingVisualizer/SortingVisualizer'
import bubbleSort from '../algorithms/bubbleSort'
function Menubar(props) {
return(
<div id="menubar-container">
<button id="new-array-button" onClick={() => newArray()}>New Array</button> // Invalid hook error
<button id="bubble-sort-button" onClick={() => bubbleSort(props.data)}>Bubble Sort</button> // Works fine
</div>
)
}
export default Menubar;
When I run "npm ls react-dom", I only get a single version returned which should mean I do not have mismatching version problems:当我运行“npm ls react-dom”时,我只返回一个版本,这应该意味着我没有版本不匹配的问题:
`-- react-dom@17.0.2
I suspect that I am breaking some hook rules but not sure which one.我怀疑我违反了一些钩子规则,但不确定是哪一个。 If I had to guess, based on another problem I saw, I think this has to do with the fact that hooks can only be used at the top level.
如果我不得不猜测,基于我看到的另一个问题,我认为这与钩子只能在顶层使用这一事实有关。 I think that somehow my onClick for newArray is misusing that hook rule.
我认为我的 newArray 的 onClick 以某种方式滥用了该钩子规则。
But something else I don't get is...shouldn't "import newArray..." be enough to use a function from another module?但是我没有得到的其他东西是......“导入newArray......”是否足以使用来自另一个模块的function? Isn't that how it works in plain javascript where one module can use the function of another module simply by importing/exporting?
这不是在普通 javascript 中的工作方式,其中一个模块只需通过导入/导出即可使用另一个模块的 function?
You can't import a function like that.您不能像这样导入 function 。 You have to pass the function to the children component.
您必须将 function 传递给子组件。
In the parent component在父组件中
// SortingVisualizer.js
export const SortingVisualizer = () => {
const [arr, setArr] = useState([]);
const newArray = () => {
// ...
}
return (
<div id="main-container">
<Menubar data={arr} newArray={newArray} />
</div>
)
}
In the children component在子组件中
// import newArray from '../SortingVisualizer/SortingVisualizer'; Remove that line
import bubbleSort from '../algorithms/bubbleSort'
const Menubar = ({ newArray }) => {
return(
<div id="menubar-container">
<button id="new-array-button" onClick={() => newArray()}>New Array</button>
<button id="bubble-sort-button" onClick={() => bubbleSort(props.data)}>Bubble Sort</button> // Works fine
</div>
)
}
To pass the function to the child component, you can pass it as a props要将 function 传递给子组件,您可以将其作为道具传递
<Menubar
data={arr}
newArray={newArray}
/>
Then in the child component, you call the function from the props然后在子组件中,从 props 中调用 function
const { newArray } = props;
return (
<div id="menubar-container">
<button id="new-array-button" onClick={newArray}>New Array</button>
I'm not sure if this is the best solution but you can create a hook too我不确定这是否是最好的解决方案,但您也可以创建一个钩子
// useArray.js
import {useState} from "react"
const useArray = () => {
const [arr,setArray] = useState([])
function newArray() {
const tempArr = [];
for(let i = 0; i < 100; i++) {
tempArr.push(Math.floor(Math.random() * 100) + 5)
}
setArr(tempArr);
}
return {
newArray,
arr
}
}
export default useArray
even if in your example I don't understand why not to put your state in your child component since your parent doesn't use it即使在你的例子中我不明白为什么不把你的 state 放在你的子组件中,因为你的父母不使用它
//Child component
import React from "react";
import bubbleSort from '../algorithms/bubbleSort'
function Menubar() {
const [arr, setArr] = useState([]);
const newArray = () => {
// ...
}
return(
<div id="menubar-container">
<button id="new-array-button" onClick={newArray}>New Array</button>
<button id="bubble-sort-button" onClick={() => bubbleSort(arr)}>Bubble Sort</button>
</div>
)
}
export default Menubar;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.