目录
jsx=>element=>component=>component tree=>application
function Welcome(){ [注意:react中函数首字母大小写]
return
hello world
}
const e =
function Welcome(props){
console.log(props);
return
hello world
}
//组件传值后,会封装 组件属性 =>对象{key:value}
const e =
注意:组件属性不能直接放到 {} 中,会报错
function Welcome(props){
console.log(props);
return
hello world {props}
} 报错:Objects are not valid as a React child
正确用法:
function Welcome(props){
console.log(props);
return
hello {props.name}
}
const e =
function Welcome(props){
return
hello {props.name}
}
const e =
hello page
babel转化:Babel · The compiler for next generation JavaScript (babeljs.io)
const e =
React.createElement("div", null,
React.createElement("h1", null, "hello page"),
React.createElement(Welcome, null),
React.createElement(Welcome, null),
React.createElement(Welcome, { name: "jack" }));
组件的虚拟DOM解析:
{
type:"div"
props:{
children:[
{
type:"h1",
props:{children:"hello page"}
},
{
type:f Welcome(props),
props:{children:"hello page"}
},
{
type: Welcome(props),
props:{}
},
{
type: Welcome(props),
props:{}
},
{
type:Welcome(props),
props:{name="jack"}
},
]
}
}
虚拟DOM=>type判断(如果是函数,则自动执行,并把props传入函数)=>真实DOM
_PURE_不可改变,只返回视图
纯函数:function fn(props){} 传入相同的参数,返回相同的值
function sum(a,b){
return a+b
}
console.log(sum(1,2)) //属于纯函数,传入相同的值,返回的值始终相同
不纯函数:传入相同的参数,可能返回不同的值,具有可变性
let p = 0
function sum2(){
a = ++p
return a+b
}
console.log(sum(1,2))
console.log(sum(1,2))

扩展:瀑布流

//需求:school - class - teachers / students - student / teacher
function Teacher(props){
let {teacher} = props
return(
)
teachers {teacher}
}
function Student(props){
let {student} = props
return (
)
student {student}
}
function Class(props){
let {name,teacher,students} = props.class1
return(
)
class : {name}
}
function School(props){
let {name,classs} = props.school1
return(
)
school {name}
}
//学校信息采集(学校 班级 老师 学生) =>数据库 =>api =>client(web) =>数据传递
let data= {
school:{
name:"一中",
classs:[
{
name:"一班",
teacher:"Mr.Wang",students:["张三","李四","王二麻子"]
},
{
name:"二班",
teacher:"Mr.Li",students:["张三","李四","王二麻子"]
},
{
name:"三班",
teacher:"Mr.Zhao",students:["张三","李四","王二麻子"]
},
]
}
}
const e =
//ReactDOM react元素(虚拟DOM)同步(优化算法)真实DOM
ReactDOM.render(e,document.getElementById("app"))
需求调研=>需求分析=>产品原型=>保真图(设计)
研发部门:技术经理=>技术分析(前台 后台 app 后端)
任务分解:具体模块
程序员:静态页面(html+css+js特效)=>react组件(大组件)=>组件抽离(小组件)=>获取接口数据(接口规范)=>数据同步到组件上
//头像复用
function Avator(props){
return (
)
}
//用户信息复用
function User(props){
return (
张三
)
}
//评论复用
function Content(props){
{/*评论内容*/}
comment content}
//日期复用
function Date(props){
{/*日期内容*/}
2022-08-20}
//重构组件 => 获取信息 =>同步数据
var data = {
author:{
name:"jack",
avator:"假装有图片地址链接"
},
content:"aaa-bbb-ccc"
date:“2022-08-20”
}
function Comment(props){
return (
)
}
const e =
ReactDOM.render(e,document.getElementById("app"))