React入门

一、React是什么?
在这里插入图片描述
一个JS库用于构建UI,有如下几个特点:

1.1 Declarative
React makes it painless to create interactive UIs. Design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes.
Declarative views make your code more predictable and easier to debug.
声明式编写UI
UI和数据绑定,当数据发生改变时同时更新渲染界面展示,类似于android中的databinding;将对象的创建工作交给React,开发者只需告诉它你想要什么,不必关心实现细节,类似于spring中的ioc。
参考:https://zhuanlan.zhihu.com/p/26085755

1.2 Component-Based
Build encapsulated components that manage their own state, then compose them to make complex UIs.
Since component logic is written in JavaScript instead of templates, you can easily pass rich data through your app and keep state out of the DOM.
基于组件
构建包装好的组件能够管理自己的状态,然后将它们组成复杂的UI界面。由于组件逻辑是用JS代码编写而不是模板代码(标记语言代码),你可以轻松的传递丰富的数据到组件中去并且将组件的状态剥离DOM。有点类似android中组合形式的自定义控件,容器型的控件不仅包裹着若干子控件,还有逻辑代码。

1.3 Learn Once, Write Anywhere
We don’t make assumptions about the rest of your technology stack, so you can develop new features in React without rewriting existing code.
React can also render on the server using Node and power mobile apps using React Native.
一次学习,任意地方使用
开发者可以在不改动现有代码的前提下开发任何新功能,在服务端与移动端皆可使用。

二、基本使用
2.1 Try React
React has been designed from the start for gradual adoption, and you can use as little or as much React as you need. Whether you want to get a taste of React, add some interactivity to a simple HTML page, or start a complex React-powered app, the links in this section will help you get started.
尝试React
React从一开始就被设计成可渐进式采用的模式。开发者可以按需使用,无论你是想尝鲜,还是往一个简单HTML中增加一些交互性,还是开启一个复杂应用,React都可以发挥它的作用。

2.2 Add React in One Minute
In this section, we will show how to add a React component to an existing HTML page. You can follow along with your own website, or create an empty HTML file to practice.
There will be no complicated tools or install requirements — to complete this section, you only need an internet connection, and a minute of your time.
在下面的例子中将展示如何往一个已存在的HMTL页面中添加React组件,你可以在自己的网站中或者创建一个空HTML文件来练习,在这个过程中不需要任何编译工具或者安装要求,只需要网络连接和几分钟时间。

Step 1: Add a DOM Container to the HTML
First, open the HTML page you want to edit. Add an empty

tag to mark the spot where you want to display something with React. For example:

<!-- ... existing HTML ... -->

<div id="like_button_container"></div>

<!-- ... existing HTML ... -->

第一步,添加一个装载虚拟DOM的容器
添加一个空<div>标记,它相当于一个占位符,后面通过React创建的组件将会填充到这里。在一个页面中你可以在任意位置放置任意数量的占位符。

Step 2: Add the Script Tags
Next, add three <script> tags to the HTML page right before the closing</body> tag:

 <!-- ... other HTML ... -->

  <!-- Load React. -->
  <!-- Note: when deploying, replace "development.js" with "production.min.js". -->
  <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>

  <!-- Load our React component. -->
  <script src="like_button.js"></script>

</body>

第二步,加载React
在</body>之前添加三个JS库引用,前两个是加载React(记住在发布到生成环境时替换成相应的生产库,生产库已经过压缩可以提高下载速度),第三个引用是加载自定义组件。

Step 3: Create a React Component
Create a file called like_button.js next to your HTML page.

'use strict';

const e = React.createElement;

class LikeButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = { liked: false };
  }

  render() {
    if (this.state.liked) {
      return 'You liked this.';
    }

    return e(
      'button',
      { onClick: () => this.setState({ liked: true }) },
      'Like'
    );
  }
}

// 这两行代码将会找到之前"id"为“like_button_container”的容器,然后将创建好的React组件塞进去
const domContainer = document.querySelector('#like_button_container');
ReactDOM.render(e(LikeButton), domContainer);

第三步,创建自定义React组件
新建一个叫“like_button.js”的文件,添加如上代码,在浏览器中运行效果如下图:
react_hello

参考:https://reactjs.org/
https://reactjs.org/docs/getting-started.html

https://reactjs.org/docs/add-react-to-a-website.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React 是一个用于构建用户界面的 JavaScript 库。它被 Facebook 开发,用于构建 Facebook 和 Instagram 等应用程序的用户界面。React 通过组件化的方式构建用户界面,使得代码更易于维护和复用。下面是一个简单的 React 入门指南: 1. 安装 React React 可以通过 NPM 安装,可以在命令行中执行以下命令来安装 React: ``` npm install react react-dom ``` 2. 创建 React 组件 在 React 中,一个组件就是一个 JavaScript 类。下面是一个简单的组件示例: ```javascript import React from 'react'; class HelloWorld extends React.Component { render() { return <div>Hello World!</div>; } } export default HelloWorld; ``` 这个组件只是简单地渲染一个 `Hello World!` 的文本。 3. 渲染组件 要在页面中渲染组件,需要使用 `ReactDOM.render()` 方法。下面是一个简单的示例: ```javascript import React from 'react'; import ReactDOM from 'react-dom'; import HelloWorld from './HelloWorld'; ReactDOM.render( <HelloWorld />, document.getElementById('root') ); ``` 这个示例中,我们首先导入了 `ReactDOM` 和 `HelloWorld` 组件,然后使用 `ReactDOM.render()` 方法将 `HelloWorld` 组件渲染到页面上。在这个示例中,我们将组件渲染到了一个 ID 为 `root` 的元素中。 这只是 React入门React 还有很多其他的概念和功能,比如 JSX、状态、生命周期等等。如果您想深入了解 React,可以查看 React 官方文档,里面有很多有用的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值