所以,我正在寻找这个问题的一些答案,发现在导入时,'React' 不需要在 {} 中,因为它是默认导出而不是命名导出,这是正确的,但我也看到了在导入默认导出时,我们可以在导入时使用任何名称。但在这种情况下,我们只能使用以下导入,
import React from 'react';
Run Code Online (Sandbox Code Playgroud)
并不是
import Somename from 'react';
Run Code Online (Sandbox Code Playgroud) 考虑下面的代码,
// Function which returns a promise and resolves in 2 seconds
const promiseFunction = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
};
// Asynchronous function 1
const asyncFunction1 = async() => {
console.log(await promiseFunction());
console.log(await promiseFunction());
console.log(await promiseFunction());
}
// Asynchronous function 2
const asyncFunction2 = async() => {
let a = promiseFunction();
let b = promiseFunction();
let c = promiseFunction();
console.log(await a);
console.log(await b);
console.log(await c);
}
Run Code Online (Sandbox Code Playgroud)
我试图了解 asyncFunction1 …
我在没有 NPM 和其他工具的情况下尝试 React,而是通过添加 CDN 链接来使用它,但是如何导入依赖包,例如 useState 钩子?如果它是通过另一个脚本标签添加的,那么相同的 CDN 链接是什么?下面是我的代码,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>React Local</title>
<script type="application/javascript" src="https://unpkg.com/react@16.0.0/umd/react.production.min.js"></script>
<script type="application/javascript" src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.production.min.js"></script>
<script type="application/javascript" src="https://unpkg.com/babel-standalone@6.26.0/babel.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const rootElement = document.getElementById('root')
const App = (props) => {
const [text, setText] = useState('hello');
return (
<div>
<h1>{text}</h1>
<input type="text" onClick={(e) => setText(e.target.value)}> </input>
</div>
);
}
ReactDOM.render(<App />, rootElement)
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在这里我会得到错误, useState 未定义。
注意:这只是为了使用直接添加到 html …