简体   繁体   English

我应该用哪种方式导入 React?

[英]Which way should I import React?

I'm not sure which way I' supposed to import React in my React Native app.我不确定应该以哪种方式在我的 React Native 应用程序中导入 React。 If I need to use a hook like useState should I import React with: import React, { useState } from 'react';如果我需要使用像useState这样的钩子,我应该使用以下useState导入 React: import React, { useState } from 'react'; , import { useState } from 'react'; , import { useState } from 'react'; , import * as React from 'react'; , import * as React from 'react'; , or import * from 'react'; , 或import * from 'react'; ? ? If I don't need any hooks should I use: import React from 'react';如果我不需要任何钩子,我应该使用: import React from 'react'; , import 'react'; , import 'react'; , import * as React from 'react'; , import * as React from 'react'; , or import * from 'react'; , 或import * from 'react'; ? ?

As of react version 17+, you don't need to import React from 'react' anymore. import React from 'react' 17+ 版本开始,您不再需要import React from 'react' Read more about the JSX transformation. 阅读有关 JSX 转换的更多信息。

Hooks are named exports, so you need to import hooks as:钩子被命名为导出,因此您需要将钩子导入为:

import {useState, useEffect /* and others */} from 'react'

If you are using a react version below 17, you need to import React .如果您使用的是低于 17 的 react 版本,则需要导入React Since React would be a default import these are all refer to the same thing:由于React将是默认导入,因此这些都是指同一件事:

import React from 'react'
import * as React from 'react'

So you should do import React, {/* hooks you are using */} from 'react' .所以你应该import React, {/* hooks you are using */} from 'react'

You can even do import * as Whatever from 'react' , since it is not a named export.您甚至可以import * as Whatever from 'react' ,因为它不是命名导出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM