欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

react-redux如何實現(xiàn)react組件之間數(shù)據(jù)共享

這篇文章主要講解了“react-redux如何實現(xiàn)react組件之間數(shù)據(jù)共享”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“react-redux如何實現(xiàn)react組件之間數(shù)據(jù)共享”吧!

創(chuàng)新互聯(lián)是一家專業(yè)提供承德企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、網(wǎng)站制作、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為承德眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。

利用react-redux實現(xiàn)react組件數(shù)據(jù)之間數(shù)據(jù)共享

1.安裝react-redux

$ npm i --save react-redux

2.從react-redux導(dǎo)入Prodiver組件將store賦予Provider的store屬性,

將根組件用Provider包裹起來。

import {Provider,connect} from 'react-redux'
ReactDOM.render(
<Provider store={store}>
 <Wrap/>
</Provider>,document.getElementById('example'))

這樣根組件中所有的子組件都可以獲得store中的值

3.connect二次封裝根組件

export default connect(mapStateToProps,mapDispatchToProps)(Wrap)

connect接收兩個函數(shù)作為參數(shù),一個mapStateToProps定義哪些store屬性會被映射到根組件上的屬性(把store傳入react組件),一個mapDispatchToProps定義哪些行為action可以作為根組件屬性(把數(shù)據(jù)從react組件傳入store)

3.定義這兩個映射函數(shù)

function mapStateToProps(state){
 return {
  name:state.name,
  pass:state.pass
 }
}
function mapDispatchToProps(dispatch){
 
 return {actions:bindActionCreators(actions,dispatch)
 }
}

把store中的name,pass映射到根組件的name,pass屬性。

actions是一個包含了action構(gòu)建函數(shù)的對象,用bindActionCreators把對象actions綁定到根組件actions屬性上。

4.在根組件引用子組件的位置用 <Show name={this.props.name} pass={this.props.pass}></Show>將store數(shù)據(jù)傳入子組件.

5.在子組件中調(diào)用actions中的方法來更新store中的數(shù)據(jù)

<Input actions={this.props.actions} ></Input>

先將actions作為屬性傳入子組件

子組件調(diào)用actions中的方法創(chuàng)建action

//Input組件
export default class Input extends React.Component{
sure(){
this.props.actions.add({name:this.refs.name.value,pass:this.refs.pass.value})
}
 render(){ 
  return (
    <div> 
  姓名:<input ref="name" type="text"/>
  密碼:<input ref="pass" type="text"/>
  <button onClick={this.sure.bind(this)}>登錄</button>
 </div>

  )
 }
}

因為我們采用了bindActionCreators函數(shù),創(chuàng)建action后會立即自動調(diào)用store.dispatch(action)將數(shù)據(jù)更新到store.

感謝各位的閱讀,以上就是“react-redux如何實現(xiàn)react組件之間數(shù)據(jù)共享”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對react-redux如何實現(xiàn)react組件之間數(shù)據(jù)共享這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

網(wǎng)站標(biāo)題:react-redux如何實現(xiàn)react組件之間數(shù)據(jù)共享
文章路徑:http://www.chinadenli.net/article34/jigdse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號ChatGPT定制開發(fā)服務(wù)器托管網(wǎng)頁設(shè)計公司搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管