gpt4 book ai didi

reactjs - 创建钩子(Hook)时真的需要导入 'React'吗? ( react 钩子(Hook))

转载 作者:行者123 更新时间:2023-12-03 13:34:10 25 4
gpt4 key购买 nike

我看到了 https://reactjs.org/docs/hooks-custom.html 的例子他们总是这样做:

import React, { useState, useEffect } from 'react';

但是文件中并没有真正使用React,我们真的需要它吗?为什么?

我问这个问题是因为我遇到了 eslint 的问题:

'React' 已定义,但从未使用过 no-unused-vars - 我使用的是 create-react-app 3.0.1,其中已包含 eslint - (我不确定如何解决这个问题 - 已经尝试过 this 并尝试将其添加到 package.json eslintConfig 但仍然没有)

最佳答案

您将需要React如果您正在渲染 JSX .

为了避免这种情况eslint警告,您应该使用 react-in-jsx-scope规则来自 eslint-plugin-react .

在该规则中,它还解释了为什么您需要 React在文件中,即使您不使用它(您认为您没有使用它,但如果您渲染 JSX ,您就会使用它)。

When using JSX, <a /> expands to React.createElement("a"). Therefore the React variable must be in scope. If you are using the @jsx pragma this rule will check the designated variable and not the React one.

关于reactjs - 创建钩子(Hook)时真的需要导入 'React'吗? ( react 钩子(Hook)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56755952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com