随笔 - 363, 文章 - 0, 评论 - 2, 阅读 - 23万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

02react基础语法

Posted on   心默默言  阅读(92)  评论(0编辑  收藏  举报

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>
        )
    }
}
复制代码

 

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-04-21 散列
2019-04-21 07数据结构——图
2019-04-21 排序
点击右上角即可分享
微信分享提示