[英]can someone tell me why this error appears to me?
错误:
React Hook "useState" is called in function "createFlight" 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
我的代码:
import React, {Component} from 'react';
import { useState } from 'react'
function createFlight(){
const [flight, setflight] = useState({flightNumber: "", departureTime: "", arrivalTime: "", departureDate: "", arrivalDate: "", cabin: "", economySeatsAvailable: "", businessSeatsAvailable: "", airport: "", from: "", to: "" });
}
export default createFlight;
将组件的名称更改为以大写字母开头(如错误所示)。
function CreateFlight(){
如果相反,它应该是一个自定义钩子,则需要在名称中use
之前将其命名。 比如使用useCreateFlight
React 钩子只能在 React 组件或其他 React 钩子中调用。
React 组件必须是大写的,就像这样
function CreateFlight(){
const [flight, setflight] = useState();
}
然后可以像这样渲染它们
<CreateFlight />
React 钩子是将行为绑定到组件中的函数。 它们必须命名为“use...”,例如“useFlight”或其他名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.