React 入门课程 - 使用CDN编程React

1. 第一个React

注意:在vscode里,使用Live Server来运行html文件。

index.html

html 复制代码
<html>
    <head>
        <link rel="stylesheet" href="index.css">
        <script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
        <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
        <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    </head>
    <body>
        <div id="root"></div>
        <script src="index.js" type="text/babel"></script>
    </body>
</html>

通过内容分发网络(CDN)可以引入React和ReactDOM库,这样可以直接在网页中使用这两个库,而不需要下载和本地存储。

  • React 是一个用于构建用户界面的JavaScript库,主要负责视图层的逻辑和组件的构建。
  • ReactDOM 是一个帮助将React组件渲染到实际DOM节点的库,负责将React的虚拟DOM映射到网页的实际DOM上。
  • crossorigin 属性的使用是为了控制在跨域请求(即从一个域访问另一个域的资源)时如何共享资源。这在从CDN加载React或其他库时尤为重要。
  • 引入一个可以在浏览器中直接使用的 Babel 版本。通常,Babel 是在构建过程中(如使用 Webpack)运行的,但 Babel Standalone 允许在不设置构建工具的情况下直接在浏览器中转换 JavaScript 代码。这在快速测试和开发原型时非常有用。
  • Babel Standalone 可以将 ES6+ 或 JSX 代码转换为旧版 JavaScript,直接在浏览器中运行。

index.js

javascript 复制代码
ReactDOM.render(<p>Hi, my name is Bob!</p>, document.getElementById("root"))
  • 将React组件渲染到页面的目标位置。通过在HTML中为React组件准备好一个空的<div>容器,然后在JavaScript代码中使用ReactDOM.render方法将组件插入该容器中。

index.css

javascript 复制代码
html, body {
    margin: 0;
    padding: 0;
}

2. React is composable

index.html, index.css不用更改,修改index.js

javascript 复制代码
function MainContent() {
    return <h1>I am learning React!</h1>
}

ReactDOM.render(<div><MainContent /></div>, document.getElementById('root'))

3. JSX

更新index.js

javascript 复制代码
const navbar = (
    <nav>
        <h1>Bob's Bistro</h1>
        <ul>
            <li>Menu</li>
            <li>About</li>
            <li>Contact</li>
        </ul>
    </nav>
)

ReactDOM.render(navbar, document.getElementById("root"))

4. 完成后的project可以部署到netlify上。

5. 添加图片

index.js

javascript 复制代码
const page = (
    <div>
        <img src="./react-logo.png" width="40px" />
        <h1>Fun facts about React</h1>
        <ul>
            <li>Was first released in 2013</li>
            <li>Was originally created by Jordan Walke</li>
            <li>Has well over 100K stars on GitHub</li>
            <li>Is maintained by Facebook</li>
            <li>Powers thousands of enterprise apps, including mobile apps</li>
        </ul>
    </div>
)

ReactDOM.render(page, document.getElementById("root"))

6. Custom Components

index.js

javascript 复制代码
function Page() {
    return (
        <ol>
            <li>It's a popular library, so I'll be 
            able to fit in with the cool kids!</li>
            <li>I'm more likely to get a job as a developer
            if I know React.</li>
        </ol>
    )
}

ReactDOM.render(<Page />, document.getElementById("root"))
javascript 复制代码
function Page() {
    return (
        <div>
            <header>
                <nav>
                    <img src="./react-logo.png" width="40px" />
                </nav>
            </header>
            <h1>Reasons I'm excited to learn React</h1>
            <ol>
                <li>It's a popular library, so I'll be 
                able to fit in with the cool kids!</li>
                <li>I'm more likely to get a job as a developer
                if I know React</li>
            </ol>
            <footer>
                <small>© 2021 Ziroll development. All rights reserved.</small>
            </footer>
        </div>
    )
}

ReactDOM.render(<Page />, document.getElementById("root"))

7. Parent/ Child Components

javascript 复制代码
function Header() {
    return (
        <header>
            <nav>
                <img src="./react-logo.png" width="40px" />
            </nav>
        </header>
    )
}

function Footer() {
    return (
        <footer>
            <small>© 2021 Ziroll development. All rights reserved.</small>
        </footer>
    )
}

function MainContent() {
    return (
        <div>
            <h1>Reasons I'm excited to learn React</h1>
            <ol>
                <li>It's a popular library, so I'll be 
                able to fit in with the cool kids!</li>
                <li>I'm more likely to get a job as a developer
                if I know React</li>
            </ol>
        </div>
    )
}

function Page() {
    return (
        <div>
            <Header />
            <MainContent />
            <Footer />
        </div>
    )
}

ReactDOM.render(<Page />, document.getElementById("root"))
相关推荐
Csvn7 分钟前
diff 算法(虚拟 DOM Reconciliation)
前端
前端snow1 小时前
ai agent --- 文件存储
前端
Frag0ut1 小时前
Chrome四大版本获取及共存指南:Stable/Beta/Dev/Canary
前端·chrome·浏览器·dev·beta·canary·共存版
IT_陈寒3 小时前
SpringBoot自动配置坑了我一把,原来是这样绕过去的
前端·人工智能·后端
广州华水科技3 小时前
单北斗GNSS变形监测在大坝安全监测中的应用与优势
前端
zeng不错3 小时前
B站 173 个投稿活动看到眼瞎?我写了个扩展,主打一个精准打击
前端
Latchh3 小时前
前端导出的JPEG红字发糊,质量要拉到100才清楚
前端·图像处理·人工智能·计算机视觉
默_笙4 小时前
🚦 请三个 AI 考官给 RAG 打分:量化评估实战(下)
前端·javascript
Joy T4 小时前
聊天系统中五大ID的演进与实战解析
java·前端·人工智能·spring·agent入门
Seraphina365 小时前
DVWA(SQL注入-low,medium,XSS反射-low)
前端·数据库·笔记·sql·网络安全·web·xss