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>
}
}
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'));
随意点击次数,标题后面的数字会跟着变化
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>
}
}
点击按钮,数组新增
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>
}
}
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>
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>
再添加一个类
函数传参 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>
08 React包使用
太长了,下篇继续写