React傳遞參數(shù)的幾種方式
父子組件之間傳遞參數(shù)
父組件往子組件傳值,直接用this.props就可以實現(xiàn)
在父組件中,給需要傳遞數(shù)據(jù)的子組件添加一個自定義屬性,在子組件中通過this.props就可以獲取到父組件傳遞過去的數(shù)據(jù)
// 父組件 render() { return ( // 使用自定義屬性傳遞需要傳遞的方法或者參數(shù) <ShopUi toson={this.state}></ShopUi> ) } //子組件 //通過this.props.toson就可以獲取到父組件傳遞過來的數(shù)據(jù)
如果還需要往孫組件傳遞那么在子組件通過自定義屬性繼續(xù)傳遞就行了
tograndson={this.props.toson}
孫組件通過this.props.tograndson獲取到數(shù)據(jù)
子組件給父組件傳值的話,需要在父組件設(shè)置接收函數(shù)和state,同時將函數(shù)名通過props傳遞給子組件
也就是給子組件傳入父組件的方法,在子組件進(jìn)行調(diào)用
//孫子組件 export default class Grandson extends Component{ render(){ return ( <div style={{border: "1px solid red",margin: "10px"}}> {this.props.name}: <select onChange={this.props.handleSelect}> <option value="男">男</option> <option value="女">女</option> </select> </div> ) } }; //子組件 export default class Child extends Component{ render(){ return ( <div style={{border: "1px solid green",margin: "10px"}}> {this.props.name}:<input onChange={this.props.handleVal}/> <Grandson name="性別" handleSelect={this.props.handleSelect}/> </div> ) } }; //父組件 export default class Parent extends Component{ constructor(props){ super(props) this.state={ username: '', sex: '' } }, handleVal(event){ this.setState({username: event.target.value}); }, handleSelect(value) { this.setState({sex: event.target.value}); }, render(){ return ( <div style={{border: "1px solid #000",padding: "10px"}}> <div>用戶姓名:{this.state.username}</div> <div>用戶性別:{this.state.sex}</div> <Child name="姓名" handleVal={this.handleVal} handleSelect={this.handleSelect}/> </div> ) } }
前一段時間有人問過我這樣一個問題,constructor里面的super()是干嘛用的?
總結(jié)一下:
如果要在子類的constructor里使用this,必須調(diào)用父類constructor,否則就拿不到this
那么問題就來了,如何調(diào)用父類的constructor呢? 通過super()
如果要在constructor里使用父組件傳遞過來的參數(shù),必須在調(diào)用父組件super時,傳遞參數(shù)給父組件的constructor
如果不在constructor里面使用this,或者參數(shù),就不需要super ; 因為React以及幫你做了this,props的綁定
路由傳參
安裝 npm install react-router-dom --save-dev
定義路由(一般會放在外面)
<HashRouter> <Switch> <Route exact path="/" component={Home}/> <Route exact path="/detail" component={Detail}/> </Switch> </HashRouter>
當(dāng)頁面跳轉(zhuǎn)時
<li onClick={el => this.props.history.push({ pathname:'/detail', state:{id:3} })} > </li>
接收 通過this.props.history.location可以獲取到傳遞過來的數(shù)據(jù)
路由傳參可能會有這個問題,就是只有在路由定義時掛載的組件中才會有props里面的location history match
路由上掛載的那個組件一般都是Container.js,一般我們會往下分出UI.js組件,在這里面進(jìn)行點擊跳轉(zhuǎn),UI組件props里沒有l(wèi)ocation history match
需要用到高階組件withRouter
狀態(tài)提升
將多個組件需要共享的狀態(tài)提升到離他們最近的那個公共父組件上,然后父組件通過props分發(fā)給子組件
context
當(dāng)某個組件在自己的context中保存了某個狀態(tài),那個該組件下的所有子孫組件都可以訪問到這個狀態(tài),不需要中間組件的傳遞,而這個組件的父組件是沒辦法訪問的
class Index extends Component { static childContextTypes = { themeColor: PropTypes.string } constructor () { super() this.state = { themeColor: 'red' } } getChildContext () { return { themeColor: this.state.themeColor } } render () { return ( <div> <Header /> <Main /> </div> ) } }
通過getChildContext()將屬性傳遞給所有的子孫組件
提供 context 的組件必須提供 childContextTypes 作為 context 的聲明和驗證。
class Title extends Component { static contextTypes = { themeColor: PropTypes.string } render () { return ( <h1 style={{ color: this.context.themeColor }}>標(biāo)題</h1> ) } }
子組件要獲取 context 里面的內(nèi)容的話,就必須寫 contextTypes 來聲明和驗證你需要獲取的狀態(tài)的類型,它也是必寫的,如果你不寫就無法獲取 context 里面的狀態(tài)。
Title 想獲取 themeColor,它是一個字符串,我們就在 contextTypes 里面進(jìn)行聲明。
引入redux
redux為React提供可預(yù)測化的狀態(tài)管理機(jī)制
redux將整個應(yīng)用狀態(tài)存儲到store,store里保存著一個state狀態(tài)樹
組件可以派發(fā)(dispatch) 行為 (action) 給store , 而不是直接通知其它組件
其它組件可以通過訂閱store中的狀態(tài)state來刷新自己的視圖
到此這篇關(guān)于React傳遞參數(shù)的幾種方式的文章就介紹到這了,更多相關(guān)React傳遞參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React+TS+IntersectionObserver實現(xiàn)視頻懶加載和自動播放功能
通過本文的介紹,我們學(xué)習(xí)了如何使用 React + TypeScript 和 IntersectionObserver API 來實現(xiàn)一個視頻播放控制組件,該組件具有懶加載功能,只有在用戶滾動頁面且視頻進(jìn)入視口時才開始下載視頻資源,需要的朋友可以參考下2023-04-04react 報錯Module build failed: Browserslis
這篇文章主要介紹了react 報錯Module build failed: BrowserslistError: Unknown browser query `dead`問題的解決方法,需要的朋友可以參考下2023-06-06Webpack 4.x搭建react開發(fā)環(huán)境的方法步驟
這篇文章主要介紹了Webpack 4.x搭建react開發(fā)環(huán)境的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08React?state結(jié)構(gòu)設(shè)計原則示例詳解
這篇文章主要為大家介紹了React?state結(jié)構(gòu)設(shè)計原則示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06react-native 實現(xiàn)購物車滑動刪除效果的示例代碼
這篇文章主要介紹了react-native 實現(xiàn)購物車滑動刪除效果的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01