1
A
回答
1
REDUX也可以使用。
- 你的精鑄件必須是純淨的,可重用的不配合他們說出 原因應用程序將只是浪費系統資源做unnessary重新 渲染。國家的使用應儘可能有限。當您使用狀態 時,您可能會冒險在組件的行爲和呈現中引入若干 (有時是微妙的)錯誤。如果您決定使用屬性來定義您的初始狀態 狀態;你的屬性可能會改變,保留你的初始狀態 根據陳舊的數據進行計算。您還需要在需要定義的屬性與組件的內部狀態之間引入緊密耦合 。
一般規則是不使用靜態組件的狀態。如果 組件不需要改變,根據外部因素,則 不使用狀態。最好在 render()方法中計算渲染值。鏈接https://www.youtube.com/watch?v=ymJOm5jY1tQ。
https://medium.com/@mweststrate/3-reasons-why-i-stopped-using-react-setstate-ab73fc67a42e#.h3ioly71l
import React, {Component} from 'react';
export const fetchResource = msg => WrappedComponent =>
class extends Component {
constructor(props){
super(props);
this.state = {
resource: null,
msg: null
};
}
componentDidMount(){
this.setState({msg})
axios.get('https://api.github.com/users/miketembos/repos')
.then((response)=>{
console.log(response);
if(response.status===200){
return response.data;
} else {
throw new Error("Server response wasn't ok");
}
})
.then((responseData)=>{
this.setState({resource:responseData});
}).catch((error)=>{
this.props.history.pushState(null, '/error');
});
}
render(){
const {resource} = this.state
//check if the resource is valid eg not empty or null
if(!resource){return <div>Loading...... or show any othe component you want load.....</div>}
return <Post {...this.props} {...resource } />
}
}
相關問題
- 1. 異步命令模式 - 異常處理
- 2. 異步方法中的異常處理
- 3. Rails異步處理模型
- 4. 從異步方法中處理事件
- 5. 異步方法與上下文處理
- 6. 如何處理異步I/O方法
- 7. 在異步方法中處理會話
- 8. 在React中處理異步請求的最佳模式是什麼?
- 9. C#異步方法未運行模式
- 10. 異步/等待方法和異常處理的最佳做法
- 11. 任務異步模式和錯誤/異常/註銷處理
- 12. 處理客戶請求時,爲什麼異步模式比同步模式好?
- 13. 處理異步第三方HTTP請求
- 14. Node.js處理異步
- 15. Java異步處理
- 16. 異步處理ODP.NET
- 17. 異常處理反模式
- 18. 異步方法無法返回無效 - 我該如何處理?
- 19. 在C#中處理異步操作的命令模式和異步操作
- 20. 異步方法中的異步方法
- 21. 異步方法?
- 22. 異步TaskManager處理作業/步異常
- 23. 異步處理圖像(Django)
- 24. Java異常處理方法
- 25. 瞭解ASP.NET異步處理
- 26. 處理異步錯誤處理
- 27. 同步處理異步輸入
- 28. React HOC,instanceof,包裝?
- 29. EJB和異步處理
- 30. dispatch_once用法用於異步處理
的代碼,請不要發佈圖片,張貼代碼,而不是。 – Bergi
React/Facebook的原始參考模式,Flux也是一個很好的起點。 Redux是從它衍生出的許多* ux模式之一。 https://facebook.github.io/flux/docs/overview.html –