導航:首頁 > 方法技巧 > react如何調用其他類的方法

react如何調用其他類的方法

發布時間:2022-06-10 01:16:37

『壹』 使用react框架怎麼進行組件的遞歸調用

JS有一種語法,叫做函數的尾遞歸(具體用法自己查)。
react組件本身不具備這些功能,需要自己用js控制業務邏輯。
例如有一個顯示評論內容的組件,當客戶端請求成功,並且返回多級json結構的時候,客戶端就需要先判斷結構有幾層,然後用js控制載入的組件層次,將數據props過去。
一般的情況下,層數只有2,參考UC的評論模式,假設我寫了一個評論,則為頂層,然後你可以在我的評論下面評論,這樣就有了2層,但是別人又可以回復你的評論,這時就要注意了,別人回復你的評論還是在第2層,也就是說,任何人的評論都只在頂層之下的同一層,不會超出2層的結構,這樣做才合理。像你說的那種,就是可以有幾十上百層評論結構,那樣顯示就不好看了。。

『貳』 reactjs怎麼在方法裡面調方法

調用函數 前加 this.
change(){
this.ff()
}
這樣

『叄』 如何在React中調用微信的jsSDK

1. 微信JSSDK使用步驟簡介

我們既然是在做基於微信的開發,當然就離不開微信的開發文檔了。開始之前希望大家能先去看下《微信JS-SDK說明文檔》。那麼我們怎麼樣才能用上微信的JSSDK呢?以下基本步驟就是基於該文檔的。

需要注意的是,如果本人下面的描述你看的有點雲里霧里的話,我建議你:

『肆』 react 父組件怎麼調用子組件的方法

可以通過向子組件傳入一個修改state的函數,比如如下代碼:
父組件:

class Father extends Component {
construtor(props){
super(props);
this.state={
name: 'Peter',
age: '26'
}
}
onChangeState(stateName){
this.setState(stateName)
}
render(){
<p>姓名:{this.state.name}</p>
<p>年齡:{this.state.age}</p>
<Child onClicked={this.onChangeState.bind(this)}/>
}
}

子組件:

class Child extends Component {
render(){
<Button onClicked={()=>this.props.onClicked({name: 'John'})}/>
}
}

『伍』 React 組件上調用方法如何傳遞參數,除了匿名函數用更好的方法嗎

Java 應用程序按值傳遞參數(引用類型或基本類型),其實都是傳遞他們的一份拷貝.而不是數據本身.(不是像 C++ 中那樣對原始值進行操作。)

例1:
Java代碼
//在函數中傳遞基本數據類型,
public class Test {

public static void change(int i, int j) {
int temp = i;
i = j;
j = temp;
}

public static void main(String[] args) {
int a = 3;
int b = 4;
change(a, b);

System.out.println("a=" + a);
System.out.println("b=" + b);
}
}

結果為:
a=3
b=4
原因就是 參數中傳遞的是 基本類型 a 和 b 的拷貝,在函數中交換的也是那份拷貝的值 而不是數據本身;

例2:
Java代碼
//傳的是引用數據類型
public class Test {

public static void change(int[] counts) {
counts[0] = 6;
System.out.println(counts[0]);
}

public static void main(String[] args) {
int[] count = { 1, 2, 3, 4, 5 };
change(count);
}
}

在方法中 傳遞引用數據類型int數組,實際上傳遞的是其引用count的拷貝,他們都指向數組對象,在方法中可以改變數組對象的內容。即:對復制的引用所調用的方法更改的是同一個對象。

『陸』 react native static怎麼調用外部方法

Annotation所修飾的對象范圍:Annotation可被用於packages、types(類、介面、枚舉、Annotation類型)、類型成員(方法、構造方法、成員變數、枚舉值)、方法參數和本地變數(如循環變數、catch參數)。在Annotation類型的聲明中使用了target可

『柒』 react中怎麼調用別人的介面進行跳轉

RN自帶了一個非常優雅的網路操作庫fetch,下面的這個例子從gankio的介面拿到了美女圖片的url然後通過state 傳給列表組件,列表裡返回圖片組件顯示圖片。網路數據獲取方法寫在componentDidMount中,這個方法是組件生命周期中需要調用的一個方法。
class AwesomeProject extends Component {// 初始化模擬數據

constructor(props) {
super(props);

const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => {r1 !== r2}});
this.state = {
dataSource: ds,
load:false,
text:''
};
}

//耗時操作放在這裡面
componentDidMount(){
this.getNet();
}

getNet(){
fetch('http://gank.io/api/search/query/listview/category/福利/count/10/page/1')//請求地址
.then((response) => response.json())//取數據
.then((responseText) => {//處理數據
//通過setState()方法重新渲染界面
this.setState({
//改變載入ListView
load: true,
//設置數據源刷新界面
dataSource: this.state.dataSource.cloneWithRows(responseText.results),
})

})
.catch((error) => {
console.warn(error);
}).done();
}

render() {
if(this.state.load){
return (
<View style={{flex: 1, paddingTop: 22}}>
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData)=>
<View>
<Image
style={{ width: 400, height: 250, marginTop: 5 }}
source={{uri:rowData.url}}/>
</View>}
/>
</View>
);
} else{
return(
<View>
<Text>loading......</Text>
</View>
);
}
}
}

『捌』 react 調用方法

情況一:
constructor(props) { super(props); this.login = this.login.bind(this);
}

login(a,b,c) { console.log(this);//列印這個組件本身
console.log(a);//列印111111
console.log(b);//undefined
console.log(c);//undefined}
<button onClick={()=>{this.login(111111)}} />

情況二:
constructor(props) { super(props); this.login = this.login.bind(this);
}

login(a,b,c) { console.log(this);//列印這個組件本身
console.log(a);//列印Proxy對象:Proxy裡面可以獲得dom元素
console.log(b);//列印event
console.log(c);//undefined}
<button onClick={this.login} />

情況三:
constructor(props) { super(props); // this.login = this.login.bind(this);}

login(a,b,c) { console.log(this);//列印這個組件本身,說明我們用箭頭函數的時候,不需要bind(this),react自動把this指向了組件自己,
console.log(a);//列印出了111111
console.log(b);//undefined
console.log(c);//undefined}
<button onClick={()=>{this.login(111111)}} />

情況四:
constructor(props) { super(props); // this.login = this.login.bind(this);}

login(a,b,c) { console.log(this);//列印null,這是react最常見的坑,this本來指向window,但是經過react初始化之後,指向了null;
console.log(a);//列印Proxy對象:Proxy裡面可以獲得dom元素
console.log(b);//列印event
console.log(c);
}
<button onClick={this.login} />

情況五:
constructor(props) { super(props); // this.login = this.login.bind(this);}

login(a,b,c) { console.log(this);//列印這個組件本身
console.log(a);//undefined
console.log(b);//undefined
console.log(c);//undefined}
<button onClick={()=>{this.login()}} />

情況六:(可以算作es5的最佳實踐,用的es5的方法,拿到所有參數)
constructor(props) { super(props); // this.login = this.login.bind(this);
this.login=(a,b,c)=>{ console.log(this);//列印這個組件本身
console.log(a);//111111
console.log(b);//列印Proxy對象:Proxy裡面可以獲得dom元素
console.log(c);//列印event:
}
}
<button onClick={this.login.bind(this,111111)} />

最佳實踐:(for es6) 老版本
constructor(props) { super(props); // this.login = this.login.bind(this);}

login(type,event,proxy) { console.log(this);//列印這個組件本身
console.log(event);//列印event:
console.log(proxy);//列印Proxy對象,event詳情請查驗es6}
<button onClick={()=>{let target=this, handler={};this.login('boss',event,new Proxy(target, handler))}}/>

最佳實踐:2018(需要傳參)
constructor(props) { super(props);
}

login=(num, evt)=>{ console.log(num);//列印傳參
console.log(evt);//列印event:}
<button onChange={this.login.bind(null, 111)}/>

最佳實踐:2018(不需要傳參)
constructor(props) { super(props);
}

login=( evt)=>{ console.log(evt);//列印event:}
<button onChange={this.login}/>

閱讀全文

與react如何調用其他類的方法相關的資料

熱點內容
帶狀皰疹中醫治療方法 瀏覽:151
換熱器的計算方法 瀏覽:138
電子秤的安裝方法圖解 瀏覽:890
三星電話儲存位置在哪裡設置方法 瀏覽:572
自製最簡單的折疊方法 瀏覽:237
15X104用簡便方法計算 瀏覽:849
excel如何轉換成word無表格的方法 瀏覽:210
高速離心機使用方法 瀏覽:737
去除手機網路緩存的方法 瀏覽:913
辣椒鹼檢測方法 瀏覽:40
烏葯種植方法 瀏覽:181
平常如何側方入庫最笨方法 瀏覽:496
踝骨軟化鍛煉方法 瀏覽:423
如何活到極致的方法 瀏覽:603
平面圖鋼筋計算方法 瀏覽:677
用什麼方法給小孩做棉褲 瀏覽:601
鰱魚怎麼做才好吃簡單的方法火鍋 瀏覽:640
和田籽料二上色的鑒別方法 瀏覽:719
聖荷納米霜使用方法 瀏覽:172
貨車多利卡打不著火解決方法 瀏覽:399