React中綁定this并給函數(shù)傳參的三種方式
前言
我們先來看下面這段代碼:
components/MyComponent.jsx
import React from "react";
export default class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg: "這是 MyComponent 組件 默認(rèn)的msg"
};
}
render() {
return (
<div>
<h1>綁定This并傳參</h1>
<input type="button" value="綁定this并傳參" onClick={this.changeMsg} />
<h3>{this.state.msg}</h3>
</div>
);
}
changeMsg() {
// 注意:這里的changeMsg()只是一個(gè)普通方法。因此,在觸發(fā)的時(shí)候,這里的 this 是 undefined
console.log(this); // 打印結(jié)果:undefined
this.setState({
msg: "設(shè)置 msg 為新的值"
});
}
}
上面的代碼中,點(diǎn)擊按鈕,執(zhí)行 changeMsg() 方法,嘗試修改 this.state.msg 的值。但是,這個(gè)方法執(zhí)行的時(shí)候,是會(huì)報(bào)錯(cuò)的:
Uncaught TypeError: Cannot read property 'setState' of null
而且,打印this的結(jié)果也是 undefined。這是為啥呢?因?yàn)檫@里的 this 并不是指向 MyComponent 組件本身。
那如何讓 changeMsg() 方法里面的 this,指向MyComponent 組件呢?辦法總是有的,比如說,將changeMsg() 修改為箭頭函數(shù):
changeMsg = () => {
console.log(this); // 打印結(jié)果:MyComponent 組件
this.setState({
msg: "設(shè)置 msg 為新的值"
});
};
那么,除了箭頭函數(shù)可以 綁定 this,還有沒有其他的方式呢?我們接下來講一講。
綁定 this 的方式一:bind()
代碼舉例:
import React from "react";
export default class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg: "這是 MyComponent 組件 默認(rèn)的msg"
};
}
render() {
return (
<div>
<h1>綁定This并傳參</h1>
{/* bind 的作用:為前面的函數(shù),修改函數(shù)內(nèi)部的 this 指向。讓 函數(shù)內(nèi)部的this,指向 bind 參數(shù)列表中的 第一個(gè)參數(shù) */}
<input
type="button"
value="綁定this并傳參"
onClick={this.changeMsg1.bind(this)}
/>
<h3>{this.state.msg}</h3>
</div>
);
}
changeMsg1() {
this.setState({
msg: "設(shè)置 msg 為新的值"
});
}
}
上方代碼中,我們?yōu)槭裁从?bind(),而不是用 call/apply 呢?因?yàn)?bind() 并不會(huì)立即調(diào)用,正是我們需要的。
注意:bind 中的第一個(gè)參數(shù),是用來修改 this 指向的。第一個(gè)參數(shù)后面的所有參數(shù),都將作為函數(shù)的參數(shù)傳遞進(jìn)去。
我們來看看通過 bind() 是怎么傳參的。
通過 bind() 綁定this,并給函數(shù)傳參:
import React from "react";
export default class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg: "這是 MyComponent 組件 默認(rèn)的msg"
};
}
render() {
return (
<div>
<h1>綁定This并傳參</h1>
{/* bind 的作用:為前面的函數(shù),修改函數(shù)內(nèi)部的 this 指向。讓 函數(shù)內(nèi)部的this,指向 bind 參數(shù)列表中的 第一個(gè)參數(shù) */}
<input type="button" value="綁定this并傳參" onClick={this.changeMsg1.bind(this, "編程的一拳超人啊", "編程的一拳超人啊")} />
<h3>{this.state.msg}</h3>
</div>
);
}
changeMsg1(arg1, arg2) {
this.setState({
msg: "設(shè)置 msg 為新的值" + arg1 + arg2
});
}
}
綁定 this 并給函數(shù)傳參 的方式二:構(gòu)造函數(shù)里設(shè)置 bind()
我們知道,構(gòu)造函數(shù)中的 this 本身就是指向組件的實(shí)例的,所以,我們可以在這里做一些事情。
代碼舉例:
import React from "react";
export default class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg: "這是 MyComponent 組件 默認(rèn)的msg"
};
// 綁定 this 并給函數(shù)傳參的方式2: 在構(gòu)造函數(shù)中綁定并傳參
// 注意:當(dāng)一個(gè)函數(shù)調(diào)用 bind 改變了this指向后,bind 函數(shù)調(diào)用的結(jié)果,有一個(gè)【返回值】,這個(gè)值,就是被改變this指向后的函數(shù)的引用。
// 也就是說: bind 不會(huì)修改 原函數(shù)的 this 指向,而是改變了 “函數(shù)拷貝”的this指向。
this.changeMsg2 = this.changeMsg2.bind(this, "編程的一拳超人恩", "編程的一拳超人恩");
}
render() {
return (
<div>
<h1>綁定This并傳參</h1>
<input type="button" value="綁定this并傳參" onClick={this.changeMsg2} />
<h3>{this.state.msg}</h3>
</div>
);
}
changeMsg2(arg1, arg2) {
this.setState({
msg: "設(shè)置 msg 為新的值" + arg1 + arg2
});
}
}
上方代碼中,需要注意的是:當(dāng)一個(gè)函數(shù)調(diào)用 bind 改變了this指向后,bind 函數(shù)調(diào)用的結(jié)果,有一個(gè)【返回值】,這個(gè)值,就是被改變this指向后的函數(shù)的引用。也就是說: bind 不會(huì)修改 原函數(shù)的 this 指向,而是改變了 “函數(shù)拷貝”的this指向。
綁定 this 并給函數(shù)傳參 的方式三:箭頭函數(shù)【薦】
第三種方式用得最多。
代碼舉例:
import React from "react";
export default class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
msg: "這是 MyComponent 組件 默認(rèn)的msg"
};
}
render() {
return (
<div>
<h1>綁定This并傳參</h1>
<input
type="button"
value="綁定this并傳參"
onClick={() => {
this.changeMsg3("編程的一拳超人3", "編程的一拳超人3");
}}
/>
<h3>{this.state.msg}</h3>
</div>
);
}
changeMsg3 = (arg1, arg2) => {
// console.log(this);
// 注意:這里的方式,是一個(gè)普通方法,因此,在觸發(fā)的時(shí)候,這里的 this 是 undefined
this.setState({
msg: "綁定this并傳參的方式3:" + arg1 + arg2
});
};
}
到此這篇關(guān)于React中綁定this并給函數(shù)傳參的三種方式的文章就介紹到這了,更多相關(guān)React綁定this并傳參內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React18系列commit從0實(shí)現(xiàn)源碼解析
這篇文章主要為大家介紹了React18系列commit從0實(shí)現(xiàn)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
React實(shí)現(xiàn)菜單欄滾動(dòng)功能
本文將會(huì)基于react實(shí)現(xiàn)滾動(dòng)菜單欄功能,點(diǎn)擊菜單,內(nèi)容區(qū)域會(huì)自動(dòng)滾動(dòng)到對(duì)應(yīng)卡片,內(nèi)容區(qū)域滑動(dòng),指定菜單欄會(huì)被選中,代碼簡(jiǎn)單易懂,感興趣的朋友一起看看吧2024-03-03
React項(xiàng)目中應(yīng)用TypeScript的實(shí)現(xiàn)
TypeScript通常都會(huì)依賴于框架,例如和vue、react 這些框架結(jié)合,本文就主要介紹了React項(xiàng)目中應(yīng)用TypeScript的實(shí)現(xiàn),分享給大家,具體如下:2021-09-09
React?高階組件與Render?Props優(yōu)缺點(diǎn)詳解
這篇文章主要weidajai?介紹了React?高階組件與Render?Props優(yōu)缺點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
React使用hook如何實(shí)現(xiàn)數(shù)據(jù)雙向綁定
這篇文章主要介紹了React使用hook如何實(shí)現(xiàn)數(shù)據(jù)雙向綁定方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

