react 常見api 使用(長期更新)

2022-07-08 21:54:22 字數 2636 閱讀 8098

父元件:
class mypage extends react.component 

我是乙個mypage);}

}

子元件:
class tabbarcomponents extends react.component ;

}// 然後使用 this.state.selectedtab 這個值, 這個就是mypage

....

}

子-》父

子元件:將子元件的value值 text 傳遞給父元件

class input extends component 

componentdidmount()

render()

}

父元件:

class mypage extends react.component ;

} changetext(event) )

} getparenttab = (tab) =>

render()

);}}

子元件通過呼叫 props.onchangetext 方法,將值傳遞進來,父元件通過 changetext 方法來接受 傳遞進來的值。

套路:子元件通過呼叫 父元件傳遞的方法 傳值。

父元件

onref = (ref) => 

addetcitem = () =>

....

子元件:

add = () => ;

componentdidmount()

說白了 也是子元件呼叫父元件的方法,將子元件的this 傳遞給父元件;

父元件使用this.child = ref 接受傳進來的 子元件, 這樣 父元件就可以使用 this.child.add() 方法了

hook 的寫法:

class hook 混合寫法:

dom:

js:uploadfiles = (e) =>

}; ...

...

upload 元件

const pictureswall = props => ) => ;

return (

);};

1.你不能用控制項的 value 或 defaultvalue 等屬性來設定表單域的值,預設值可以用 form 裡的 initialvalues 來設定。注意 initialvalues 不能被 setstate 動態更新,你需要用 setfieldsvalue 來更新。

2.你不應該用 setstate,可以使用 form.setfieldsvalue 來動態改變表單值。

!!!先setstate 修改 state的值,再次setfieldsvalue 的值

formref = react.createref();

state =

}....

this.setstate()

this.formref.current.setfieldsvalue();

// 或者 重新設定下表單 this.formref.current.resetfields();

....

this.formref && this.formref.current && this.formref.current.setfieldsvalue(this.state.initdata);

...

重置表單內容的時候: this.formref.current.resetfields(); 不奏效,

再次使用

this.formref.current.setfieldsvalue(,

});重置

hook. 經過測試 可以實現 新增和編輯(多個) 在同乙個 from 表單

const [form] = form.useform();

useeffect(() => , [props.modaldata])

....

參考:

一下是需要的資料格式

field.name 為數字遞增 則

weightlist = [

,]

field.name 為不為數字遞增切不相同 則

weightlist = [

[fname]: ,

[fname]: ,

]

常見的我們都需要乙個純陣列值,所以 this.state.weightlist 陣列裡面的每個 item 有個屬性name (可以是其他屬性名)數字遞增或者不相

ros常見錯誤及解決辦法 長期更

這時我們需要在py程式前加上 usr bin env python目的是在執行python指令碼的時候告訴作業系統我們要用python直譯器去執行py指令碼 echo source catkin ws devel setup.bash bashrc source bashrccatkin ws是當前...

react常見的樣式的使用

在css檔案中寫入樣式.title 在元件中引入import style.css 使用classname title h2h2 2.引入styled 定義import styled from styled components const myh1 styled.h1 font size 88px ...

Python 常見函式的基本使用(邊學邊更)

最近在學習python,所謂好記性不如爛筆頭故藉這次學習機會做個筆記,方便今後快速再學習。以下是常見函式的使用說明 該函式用於建立數列,根據引數個數不同對應不同的用法進行說明 1 range a,b,c 三個引數時,表示建立乙個從a b 1的陣列,每c個數取乙個值。當c 1時,可省略c,即用法等同於...