1.react基础
1.1react同步异步
import React, { Component } from 'react' export default class App extends Component { state = { count: 1 } render() { return ( <div> {this.state.count} <button onClick={this.handleAdd1}>add1</button> <button onClick={this.handleAdd2}>add2</button> </div> ) } handleAdd1 = () => { this.setState({ count: this.state.count + 1 }) console.log(this.state.count) this.setState({ count: this.state.count + 1 }) console.log(this.state.count) this.setState({ count: this.state.count + 1 },()=>console.log(this.state.count)) } handleAdd2 = () => { setTimeout(() => { this.setState({ count: this.state.count + 1 }) console.log(this.state.count) this.setState({ count: this.state.count + 1 }) console.log(this.state.count) this.setState({ count: this.state.count + 1 }) console.log(this.state.count) }, 0); } } /* setState处在同步的逻辑中,异步更新状态,更新真实dom setState处在异步的逻辑中,同步更新状态,更新真实dom setState 接受第二个参数,第二个参数是回调函数,状态和dom更新完后就会被触发 */
1.2属性props
import React, { Component } from 'react' import Navbar from './Navbar' export default class App extends Component { render() { return ( <div> <div> <h2>首页</h2> <Navbar title="首页" leftshow={false}></Navbar> {/* <Navbar title="首页" leftshow="false"></Navbar> 不用用引号,引号表示字符串 */} </div> <div> <h2>列表</h2> <Navbar title="列表" leftshow={true}></Navbar> </div> <div> <h2>购物车</h2> <Navbar title="购物车" leftshow={true}></Navbar> </div> </div> ) } }
1.3属性验证
import React, { Component } from 'react' import Navbar from './Navbar' export default class App extends Component { render() { return ( <div> <div> <h2>首页</h2> <Navbar title="首页" leftshow={false}></Navbar> {/* <Navbar title="首页" leftshow="false"></Navbar> 不用用引号,引号表示字符串 */} </div> <div> <h2>列表</h2> <Navbar title="列表" ></Navbar> </div> <div> <h2>购物车</h2> <Navbar title="购物车" leftshow={true}></Navbar> </div> </div> ) } }
import React, { Component } from 'react' import zzPropTypes from 'prop-types' console.log(zzPropTypes) export default class Navbar extends Component { //属性验证 static propTypes = { title: zzPropTypes.string, leftshow: zzPropTypes.bool } render() { console.log(this.props) let { title, leftshow } = this.props return ( // <div>Navbar-{this.props.title}</div> <div> {leftshow && <button>返回</button>} Navbar-{title} </div> ) } } //类属性 //参数类型验证 // Navbar.propTypes = { // title: zzPropTypes.string, // leftshow: zzPropTypes.bool // }
1.4默认属性
import React, { Component } from 'react' import zzPropTypes from 'prop-types' console.log(zzPropTypes) export default class Navbar extends Component { //属性验证 static propTypes = { title: zzPropTypes.string, leftshow: zzPropTypes.bool } //默认属性 static defaultProps = { leftshow: true } render() { console.log(this.props) let { title, leftshow } = this.props return ( <div> {leftshow && <button>返回</button>} Navbar-{title} </div> ) } } //默认值 // Navbar.defaultProps = { // leftshow: true // }
1.5对象属性自动展开
import React, { Component } from 'react' import Navbar from './Navbar' export default class App extends Component { render() { var obj = { title: "nihao", leftshow: false } return ( <div> <div> <h2>首页</h2> <Navbar title="首页" leftshow={false}></Navbar> {/* <Navbar title="首页" leftshow="false"></Navbar> 不用用引号,引号表示字符串 */} </div> <div> <h2>列表</h2> <Navbar title="列表" ></Navbar> </div> <div> <h2>购物车</h2> <Navbar title="购物车" leftshow={true}></Navbar> </div> <div> <h2>购物车2</h2> {/* 对象属性自动展开,与python中的字典展开一致 */} <Navbar {...obj}></Navbar> </div> </div> ) } }
1.6函数式组件
import React from 'react' export default function SideBar(props) { console.log(props) let { bg, position } = props return ( <div style={ { background: bg, width: "200px", position: "fixed", right: 0 }}> <ul> <li>11111</li> <li>11111</li> <li>11111</li> <li>11111</li> <li>11111</li> <li>11111</li> <li>11111</li> </ul> </div> ) }
import React from 'react' export default function SideBar(props) { console.log(props) let { bg, position } = props return ( <div style={ { background: bg, width: "200px", position: "fixed", right: 0 }}> <ul> <li>11111</li> <li>11111</li> <li>11111</li> <li>11111</li> <li>11111</li> <li>11111</li> <li>11111</li> </ul> </div> ) }
import React, { Component } from 'react' import zzPropTypes from 'prop-types' console.log(zzPropTypes) export default class Navbar extends Component { //属性验证 static propTypes = { title: zzPropTypes.string, leftshow: zzPropTypes.bool } //默认属性 static defaultProps = { leftshow: true } render() { console.log(this.props) let { title, leftshow } = this.props return ( <div> {leftshow && <button>返回</button>} Navbar-{title} </div> ) } } //默认值 // Navbar.defaultProps = { // leftshow: true // }
1.7状态vs属性
import React, { Component } from 'react' class Child extends Component { render() { return <div> child-{this.props.text} {/* 子组件修改属性会报错 */} {/* <button onClick={() => { this.props.text = "333333" }} >click 子</button> */} </div> } } export default class App extends Component { state = { text: "1111111" } render() { return ( <div> <button onClick={() => { // 属性可以由父组件修改 this.setState( { text: '2222' } ) }}>click 父</button> {/* 属性能够从父组件获取 */} <Child text={this.state.text} /> </div> ) } }
2.通信
2.1子传父
import React, { Component } from 'react' class Navbar extends Component { render() { return <div style={{ background: "red" }}> <button onClick={() => { console.log("子通知父,让父的siShow取反") this.props.event() //调用父组件提供的函数来修改父组件中的变量,间接 }}>click</button> <span>Navbar</span> </div> } } class Sidebar extends Component { render() { return <div style={{ background: "yellow" }}> <ul> <li>111111</li> <li>111111</li> <li>111111</li> <li>111111</li> <li>111111</li> </ul> </div> } } export default class App extends Component { state = { isShow: false } handleEvent = () => { console.log("父组件定义的event事件") this.setState({ isShow: !this.setState.isShow }) } render() { return ( <div> <Navbar event={this.handleEvent} /> {this.state.isShow && <Sidebar />} </div> ) } } /* Siderbar隐藏和显示 父传子:属性 子传父:回调函数,父类提供一个方法,子类调用 */
2.2父子通信版表单域(受控组件)
import React, { Component } from 'react' class Field extends Component { render() { return <div style={{ background: "yellow" }}> <label>{this.props.label}</label> <input type={this.props.type} onChange={(evt) => { this.props.onChangeEvent(evt.target.value) }} value={this.props.value} /> </div> } } export default class App extends Component { state = { // username: "", username: localStorage.getItem("username"), password: "" } render() { return ( <div> <h1>登陆页面</h1> <Field label="用户名" type="text" onChangeEvent={(value) => { // console.log(value) this.setState({ username: value }) }} value={this.state.username} /> <Field label="密码" type="password" onChangeEvent={(value) => { // console.log(value) this.setState({ password: value }) }} value={this.state.password} /> <button onClick={() => { console.log(this.state.username, this.state.password, '发送后端验证') }}>登陆</button> <button onClick={()=>{ this.setState({ username:"", password:"" }) }}>取消</button> </div> ) } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2019-04-21 散列
2019-04-21 07数据结构——图
2019-04-21 排序