思想主要还是来源于面向过程和面向对象的编程思想,将 UI 拆分为独立可复用的代码片段,并对每个片段进行独立构思后封装到单独的js文件当中,借助于es6的导入导出语法,可以在项目中需要用到的地方导入即可使用,有效提高的代码的复用性。
react中声明组件可以声明为函数组件,也可以声明为类组件
这里先把主要概念都提一下, 后面再进行详细讲解以及实例演示说明
这个对象特别关键,React中的几个核心机制都是围绕props对象展开的,这个props对象是数据传递及数据获取中特别关键的成员。
组件间的数据传递
数据流向下单向流动的实现
路由参数的获取
父级组件数据获取
资源路径的获取
import React from 'react';
import ReactDOM from 'react-dom';
function Index () {
return (
<div>
<h3>这是一个函数组件</h3>
</div>
)
}
ReactDOM.render(<Index />, document.getElementById('root'))
注意事项:
import React from 'react';
import ReactDOM from 'react-dom';
// 1、定位到DOM对象
let el = document.getElementById('root')
// 2、初始化数据
const author = 'zhangsan'
const age = 20
// const student = {author: 'zhangsan', age: 20}
// 3、使用构造函数创建挂载组件
function Message(props) {
return (
<div>
{
/* 直接使用jsx的插值语法绑定数据 */}
<h3>{
author}</h3>
<h3>{
age}</h3>
<hr />
{
/* 通过props接收参数后再绑定数据 */}
<h3>{
props.author}</h3>
<h3>{
props.age}</h3>
</div>
)
}
// 4、组件挂载到目标DOM并渲染
ReactDOM.render(<Message author={
author} age={
age} />, el);
说明
// 封装的自定义组件中必须要引入React, 因为jsx形式的数据结构必须要是用React才能正常解析
import React from 'react';
// 自定义组件如果要其他组件可以调用,必须使用export进行导出
export function Message(props) {
return (
<div>
<h3>{
props.author}</h3>
<p>{
props.description}</p>
</div>
)
}
import React from 'react';
import ReactDOM from 'react-dom';
import {
Message } from '../views/components/message'
// 使用单独的文件封装组件
// 1、定位到DOM对象
let el = document.getElementById('root')
// 2、初始化数据
const author = 'zhangsan'
const age = 20
const description = 'zhangsan is a very ordinary name...'
// const student = {author: 'zhangsan', age: 20}
// 4、组件挂载到目标DOM并渲染
ReactDOM.render(<Message author={
author} age={
age} description={
description} />, el);
注意事项
类组件的定义规范
import React, {
Component } from 'react';
import ReactDOM from 'react-dom';
// 1、定位到DOM对象
let el = document.getElementById('root')
// 2、定义类组件
class Message extends Component {
render () {
return (
<div>
<h3>zhangsan</h3>
<p>20</p>
<p>'zhangsan is a very ordinary name...'</p>
</div>
)
}
}
// 3、组件挂载到目标DOM并渲染
ReactDOM.render(<Message />, el);
两种方式说明
import React, {
Component } from 'react';
import ReactDOM from 'react-dom';
// 1、定位到DOM对象
let el = document.getElementById('root')
// 2、初始化数据
const author = 'zhangsan'
const age = 20
const description = 'zhangsan is a very ordinary name...'
// const student = {author: 'zhangsan', age: 20}
// 3、定义类组件
class Message extends Component {
render () {
return (
<div>
{
/* 如果在同一个文件中,可以直接使用jsx中的插值语法进行赋值 */}
<h3>{
author}</h3>
<p>{
age}</p>
<p>{
description}</p>
<hr />
{
/* 直接使用this.props获取数据, props数据依然为只读的 */}
<h3>{
this.props.author}</h3>
<p>{
this.props.age}</p>
<p>{
this.props.description}</p>
</div>
)
}
}
// 4、组件挂载到目标DOM并渲染
ReactDOM.render(<Message author={
author} age={
age} description={
description} />, el);
import React, {
Component } from "react";
export class Notice extends Component {
render () {
return (
<div>
<h3>{
this.props.author}</h3>
<p>{
this.props.age}</p>
<p>{
this.props.description}</p>
</div>
)
}
}
import React from 'react';
import ReactDOM from 'react-dom';
import {
Notice } from '../views/components/notice'
// 使用单独的文件封装自定义类组件
// 1、定位到DOM对象
let el = document.getElementById('root')
// 2、初始化数据
const author = 'gutianle'
const age = 24
const description = 'gutianle is a very ordinary name...'
// 3、组件挂载到目标DOM并渲染
ReactDOM.render(<Notice author={
author} age={
age} description={
description} />, el);
注意事项
import React, {
Component } from 'react'
// 两种创建组件方式的对比
// 1. 用构造函数创建出来的组件:无状态组件;
// 2. 用class关键字创建出来的组件:有状态组件;
// 3. 什么情况下使用有状态组件?什么情况下使用无状态组件?
// 如果组件内,不需要有私有的数据,此时,使用构造函数创建无状态组件比较合适;
// 如果组件内,需要有自己私有的数据,则,使用 class 关键字 创建有状态组件比较合适;
// 组件中的 props 和 state 之间的区别
// props 中存储的数据,都是外界传递到组件中的;
// props 中的数据,都是只读的;
// state 中的数据,都是组件内私有的;
// state 中的数据,都是可读可写的;
// props 在 有状态组件 和 无状态组件中,都有;
// state 只有在 有状态组件中才能使用;无状态组件中,没有 state;
export class MsgList extends Component {
// 在构造函数中接收外部传递的数据
constructor (props) {
super(props);
// 通过state属性声明组件中的私有数据
this.state = {
sex: 'male',
username: '青青子衿'
}
}
render () {
return (
<div>
<h3>{
this.props.author}</h3>
<p>{
this.props.age}</p>
<p>{
this.state.sex}</p>
<p>{
this.state.username}</p>
<p>{
this.props.description}</p>
</div>
)
}
}