Reactjs 将reactj与Redux和hook一起使用
我有这个部分:Reactjs 将reactj与Redux和hook一起使用,reactjs,redux,react-hooks,Reactjs,Redux,React Hooks,我有这个部分: import React from 'react'; import { useSelector, useDispatch } from 'react-redux'; import inputAction from '../../redux/actions/inputActions'; import clienteActions from '../../redux/actions/clientesAction'; const clientes = () => {
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import inputAction from '../../redux/actions/inputActions';
import clienteActions from '../../redux/actions/clientesAction';
const clientes = () => {
const dispatch = useDispatch();
const clientes = useSelector(state => state.clientes.clientes);
//.... more code
}
ant我在这行有一个错误:
const dispatch = useDispatch();
这就是错误所在
src\components\clientes\clientes.js
Line 7:22: React Hook "useDispatch" is called in function "clientes" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter react-hooks/rules-of-hooks
Line 8:22: React Hook "useSelector" is called in function "clientes" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter react-hooks/rules-of-hooks
我错了什么?所有React组件名称必须以大写字母开头。如果组件名称以小写字母开头,它将被视为类似div或span的内置元素。这是因为JSX的工作方式
只需将函数更改为客户,不要忘记导出它。react功能组件中的挂钩必须在主组件函数中使用,无条件、循环或回调 useState钩子的示例:
function Example() {
const [count, setCount] = useState(0);
return (
<div>
Text
</div>
);
}
函数示例(){
const[count,setCount]=useState(0);
返回(
正文
);
}
您必须将useDispatch hook移动到主组件函数重命名组件,并且组件名称必须为大写 例如:
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import inputAction from '../../redux/actions/inputActions';
import clienteActions from '../../redux/actions/clientesAction';
export const Clientes = () => {
const dispatch = useDispatch();
const clientes = useSelector(state => state.clientes.clientes);
//.... more code
}
我认为这个问题是不言自明的。在您的错误消息中,他们说的是
。。。React组件名称必须以大写字母开头…
。因此,您的组件目前被认为是正常功能,但它是一个组件。因此,如果您想让它成为react组件,它必须以大写字母开头
const Clientes = () => {
...
}
您的代码看起来很好,除了一件事:您的组件名称是小写的,这导致了错误。在react中,组件(即函数名)必须以大写字母开头。您可以了解更多信息,以及为什么会出现这种情况您的功能
客户
是否返回一些元素?它是如何呈现的?客户
不被视为反应组件-是!!!我有一个return@chazsolo它的工作,我改变了名称功能和它的工作感谢!真管用!谢谢