本文小編為大家詳細(xì)介紹“react怎么更新state”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“react怎么更新state”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、北海街道網(wǎng)站維護(hù)、網(wǎng)站推廣。
react更新state方法有:1、通過key變化子組件,代碼如“<Children key={this.state.key} a={this.state.a} b={this.state.b} />”;2、利用ref父組件調(diào)用子組件函數(shù);3、通過父級給子級傳數(shù)據(jù),子級只負(fù)責(zé)渲染。
react中父級props改變,更新子級state的多種方法
子組件:
class Children extends Component {
constructor(props) {
super(props);
this.state = {
a: this.props.a,
b: this.props.b,
treeData: '',
targets: '',
}
}
componentDidMount() {
const { a, b } = this.state
const data = {a,b}
fetch('/Url', {
data
}).then(res => {
if (res.code === 0) {
this.setState({
treeData: res.a,
targets: res.b,
})
} else {
message.error(res.errmsg)
}
})
}
test(item1, item2) {
const data = { item1, item2 }
fetch('/Url', {data}).then(res => {
if (res.code === 0) {
this.setState({
treeData: res.a,
targets: res.b,
})
} else {
message.error(res.errmsg)
}
})
}
}
export default Children
方法一:巧用key
<Children key={this.state.key} a={this.state.a} b={this.state.b} /> //父組件調(diào)用子組件
這種方法是通過key變化子組件會重新實(shí)例化 (react的key變化會銷毀組件在重新實(shí)例化組件)
方法二:利用ref父組件調(diào)用子組件函數(shù)(不符合react設(shè)計(jì)規(guī)范,但可以算一個(gè)逃生出口嘻嘻~)
class father extends Component {
constructer(props) {
super(props);
this.state={
a: '1',
b: '2',
}
this.myRef
this.test = this.test.bind(this)
}
change() {
const { a,b } = this.state
console.log(this.myRef.test(a,b)) // 直接調(diào)用實(shí)例化后的Children組件對象里函數(shù)
}
render() {
<Children wrappedComponentRef={(inst) => { this.myRef = inst } } ref={(inst) => { this.myRef = inst } } />
<button onClick={this.test}>點(diǎn)擊</button>
}
}
注:wrappedComponentRef是react-router v4中用來解決高階組件無法正確獲取到ref( 非高階組件要去掉哦)
方法三:父級給子級傳數(shù)據(jù),子級只負(fù)責(zé)渲染(最符合react設(shè)計(jì)觀念)推薦?。?/p>
父組件:
class father extends Component {
constructer(props) {
super(props);
this.state={
a:'1',
b:'2',
data:'',
}
}
getcomposedata() {
const { a, b } = this.state
const data = { a, b }
fetch('/Url', {data}).then(res => {
if (res.code === 0) {
this.setState({
data:res.data
})
} else {
message.error(res.errmsg)
}
})
}
render() {
<Children data={this.state.data}} />
}
}
子組件:
componentWillReceiveProps(nextProps) {
const { data } = this.state
const newdata = nextProps.data.toString()
if (data.toString() !== newdata) {
this.setState({
data: nextProps.data,
})
}
}
注:react的componentWillReceiveProps周期是存在期用改變的props來判斷更新自身state
讀到這里,這篇“react怎么更新state”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁標(biāo)題:react怎么更新state
文章轉(zhuǎn)載:http://chinadenli.net/article34/jsiope.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、定制網(wǎng)站、網(wǎng)站收錄、移動網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)