React框架——入门知识点总结+示例(2)

05 事件书写

1.事件绑定和this指向

绑定事件:on+事件名字Click(首字母大写)/ onMouseEnter={this.函数名}

<body>
    <div id="app">
        
    </div>
    <script type="text/babel">
    //1.类定义组件
		class Child extends React.Component{
        constructor(){
            super()
        }
        change(){
            console.log(111);
            //4.事件是异步操作,执行事件的时候,this不指向类
            console.log(this); //undefined
        }
        render(){
        	console.log(this); //类里面的this,一般指向当前实例化的对象;这里可以理解为this指向的是使用类相关的参数(与类有关的对象)
            return <div>
                //3.使用事件
                <button onClick={this.change} onMouseEnter={this.change}>按钮</button>
            </div>
        }
    }
	//2.渲染标签
    ReactDOM.render(<Child/>,document.getElementById('app'));
	</script>
</body>
2.改变this指向

普通函数的this指向–>Window
构造函数的this指向–>实例化对象

call、apply、bind的作用是改变函数运行时this的指向:
参考文章:https://www.cnblogs.com/pssp/p/5215621.html

<body>
    <div id="app">
        
    </div>
    <script type="text/babel">
    //1.类定义组件
		class Child extends React.Component{
        constructor(){
            super()
        }
        //用箭头函数改变this指向
        change=()=>{
            console.log(this); 
        //Child {props: {…}, context: {…}, refs: {…}, updater: {…}, change: ƒ, …}
        }
        render(){
            return <div>
                //3.使用事件
                <button onClick={this.change} onMouseEnter={this.change}>按钮</button>
            </div>
        }
    }
	//2.渲染标签
    ReactDOM.render(<Child/>,document.getElementById('app'));
	</script>
</body>
3.给标签取 ref 值(输入值可获取里面的值)

ref是一个集合,存放取过名字的标签,通过this.refs.ref(名字).value/innerHTML值获取标签;

例:<input type=“text” ref=“txt”/ >

<body>
    <div id="app">
        
    </div>
    <script type="text/babel">
    //1.类定义组件
		class Child extends React.Component{
        constructor(){
            super()
        }
        //用箭头函数改变this指向
        change=()=>{
        //4.this.refs.txt.value/innerHTML
          console.log(this.refs.txt.value,this.refs.btn.innerHTML);
        }
        render(){
            return <div>
            //3.ref
             	<input type="text" ref="txt"/>
             	<button onClick={this.change} ref="btn"></button>
            </div>
        }
    }
	//2.渲染标签
    ReactDOM.render(<Child/>,document.getElementById('app'));
	</script>
</body>

06 state

1.使用原因:props参数的值不能修改
 class List extends React.Component{
        constructor(props) {
            super(props);
        }
        change=()=>{
            //props参数的值不能修改
            this.props.num=this.props.num+1
            }
        render(){
            return <div>
                <button onClick={this.change}>这是数据{this.props.num}</button> 
            </div>
        }
    }

    ReactDOM.render(<List num="1" />,document.getElementById('app'))
2.所以:使用state
class Div extends React.Component{
    constructor(props) {
        super(props);
        //this.state存放的是私有数据 可以直接修改
        this.state={
            msg:0
        }
    }

    render(){
        return <div>
            <h1>这是标题{this.props.num}{this.state.msg}</h1>
        </div>
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fOHnVMTz-1596477946876)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20200804001304503.png)]

3.修改state的值(点击标题修改)
class Div extends React.Component{
    constructor(props) {
        super(props);
        //this.state存放的是私有数据 可以直接修改
        this.state={
            msg:0
        }
    }
    change=()=>{
        this.setState({
            msg:++this.state.msg
        })
    }
    render(){
        return <div>
            <h1 onClick={this.change}>这是标题{this.state.msg}</h1>
        </div>
    }
}
 ReactDOM.render(<Div/>,document.getElementById('app'));

随意点击次数,标题后面的数字会跟着变化
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DJ65pKjM-1596476938043)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20200804001708743.png)]

4.使用state里的数组
class Div extends React.Component{
    constructor() {
        super();
        //this.state存放的是私有数据
        this.state={
            msg:0,
            a:[1,2,3,4],
            //info数组
            info:[
                {
                    "msg":'昨天天气'
                },
                {
                    "msg":'昨天天气'
                },
                {
                    "msg":'昨天天气'
                },
                {
                    "msg":'昨天天气'
                }
            ]
        }
    }
    change=()=>{
        //修改数据
        this.setState({
            msg:++this.state.msg
        })
    }
    render(){
        return <div>
            <h1>这是标题{this.state.msg}{this.state.a}</h1>
            {
                //map循环
                this.state.info.map((item,index)=>{
                    return <p key={index}>{item.msg}</p>
                })
            }
        </div>
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ca7THKHT-1596476938049)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20200804002833818.png)]
点击按钮,数组新增

class Div extends React.Component{
    constructor() {
        super();
        //this.state存放的是私有数据
        this.state={
            msg:0,
            a:[1,2,3,4],
            info:[
                {
                    "msg":'昨天天气'
                },
                {
                    "msg":'昨天天气'
                },
                {
                    "msg":'昨天天气'
                },
                {
                    "msg":'昨天天气'
                }
            ]
        }
    }
    change=()=>{
        //数组新增,直接调用方法
        this.state.info.unshift({msg:'新增的'})
        //普通数据修改
        this.setState({
            msg:++this.state.msg
        })
    }
    render(){
        return <div>
            <h1>这是标题{this.state.msg}{this.state.a}</h1>
            <button onClick={this.change}>按钮</button>
            {
                this.state.info.map((item,index)=>{
                    return <p key={index}>{item.msg}</p>
                })
            }
        </div>
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4Cjt9q4L-1596476938068)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20200804003608932.png)]

07事件传参

1.函数传参:this.事件名.bind(this,实参1,实参2)
<script type="text/babel">
        class Div extends React.Component{
        constructor() {
            super();
            //this.state存放的是私有数据
            this.state={
                msg:0,
                a:[1,2,3,4]
            }
        }
        change=()=>{
       
        }
        render(){
            return <div>
                <h1>这是标题</h1>
                {
                    this.state.a.map((item,index)=>{
                        //函数传参 this.事件名.bind(this,实参1,实参2)
                        return <p key={index} onClick={this.change.bind(this,index)}>{item}</p>
                    })
                }
            </div>
        }
    }

    ReactDOM.render(<Div/>,document.getElementById('app'));

</script>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-32JR8dXV-1596476938074)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20200804010215675.png)]

2.声明方法(形参1,形参2,event)

最好有几个形参就有几个实参

<script type="text/babel">
        class Div extends React.Component{
        constructor() {
            super();
            //this.state存放的是私有数据
            this.state={
                msg:0,
                a:[1,2,3,4]
            }
        }
        change=(a,c,e,d)=>{
            //d没有对应的参数,这个时候d是undefined
       		console.log(e)
        }
        render(){
            return <div>
                <h1>这是标题</h1>
                {
                    this.state.a.map((item,index)=>{
                        //函数传参 this.事件名.bind(this,实参1,实参2)
                        //声明方法(形参1,形参2,event)
                        return <p key={index} onClick={this.change.bind(this,index,1)}>{item}</p>
                    })
                }
            </div>
        }
    }

    ReactDOM.render(<Div/>,document.getElementById('app'));

</script>
3.添加className
<style>
        .current{
            background-color: lightcoral;
        }
</style>

<script type="text/babel">
	class Div extends React.Component{
        constructor() {
            super();
            //this.state存放的是私有数据
            this.state={
                msg:0,
                a:[1,2,3,4]
            }
        }
        change=(a,e)=>{
            this.setState({
                msg:a
            })
        }
        render(){
            return <div>
                <h1>这是标题</h1>
                {
                    this.state.a.map((item,index)=>{
                    //函数传参 this.事件名.bind(this,实参1,实参2)
                    //声明方法 (形参1,形参2,event)
                    return <p key={index} onClick={this.change.bind(this,index)} className={index==this.state.msg?'current':''}>{item}</p>
                    })
                }
            </div>
        }
    }
</script>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SYong1He-1596476938077)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20200804012850280.png)]

再添加一个类

函数传参 this.事件名.bind(this,实参1,实参2)
声明方法 (形参1,形参2,event)

<style>
        .current{
            background-color: lightcoral;
        }
/*新添加的类*/
		.one{
            line-height: 60px;
            cursor: pointer;
        }
</style>

<script type="text/babel">
        class Div extends React.Component{
        constructor() {
            super();
            //this.state存放的是私有数据
            this.state={
                msg:0,
                a:[1,2,3,4]
            }
        }
        change=(a,e)=>{
            this.setState({
                msg:a
            })
        }
        render(){
            return <div>
                <h1>这是标题</h1>
                {
                    this.state.a.map((item,index)=>{
                        //有两个类 one一定有
                        return <p key={index} onClick={this.change.bind(this,index)} className={index==this.state.msg?'current one':'one'}>{item}</p>
                    })
                }
            </div>
        }
    }
</script>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sGeVTwk0-1596476938082)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20200804013201811.png)]

08 React包使用

太长了,下篇继续写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值