我有競賽部分,它重複所有來自數據庫的比賽。當用戶點擊其中的一個時,它會將他重定向到競賽頁面,加載一秒鐘並呈現包含所有細節的頁面。到現在爲止還挺好。如何在用戶輸入容器時使用React/Redux刷新道具
但是當用戶回到比賽組,然後點擊第二場比賽時,它會立即加載上一場比賽,加載時間爲0。
從我的角度來看,失敗的是組件的道具在我渲染組件時(第二次)沒有更新。是不是一個路由器的問題,這是我的第一本能,因爲我看到route.params按照變化,但我派遣改變道具的行動不派遣。以下是該組件的一些代碼。
class CompetitionPage extends React.Component {
componentWillMount() {
let id = getIdByName(this.props.params.shortname)
this.props.dispatch(getCompAction(id));
this.props.dispatch(getCompMatches(id));
this.props.dispatch(getCompParticipants(id));
this.props.dispatch(getCompBracket(id));
}
render() {
let { comp, compMatches, compBracket, compParticipants } = this.props
...
我試過我知道的每個生命週期方法。組件將/不會掛載,組件將/更新,我甚至將shouldUpdate設置爲true,並沒有這樣做。據我所知,問題將通過一種生命週期方法來解決,以便每次用戶進入比賽頁面時分發動作,而不僅僅是第一次。我在這裏沒有選擇,所以任何幫助將不勝感激。
注意:我是React/Redux的新手,所以我知道有一些東西有反模式/做得不好。
更新:新增CompetitionsSection
class CompetitionsSection extends React.Component {
render() {
const {competitions} = this.props;
return (
...
{ Object.keys(competitions).map(function(comp, i) {
return (
<div key={i} className={competitions[comp].status ===
undefined? 'hide-it':'col-xs-12 col-md-6'}>
...
<Link to={"/competitions/"+competitions[comp].shortName}>
<RaisedButton label="Ver Torneo" primary={true} />
</Link>
...
您可以發佈您'CompetitionSection'代碼?我最初的猜測是,你的'CompetitionPage'只是更新,而不是卸載/重新安裝,但我必須看到代碼確定 – taylorc93
你試過'componentWillReceiveProps(nextProps)'嗎?也許這裏沒有更新... – Brejk
@ taylorc93如何確保'CompetitionPage'卸載/ remound組件?我會更新這個問題並添加一些代碼。 – user3889291