2017-09-14 82 views
0

我有一个奇怪的问题,我的反应,REDX应用程序崩溃时,我重新呈现组件。我正在谈论的这个组件,DoorsSettingsContainer。它有它自己的路径:重新呈现|时组件会崩溃反应,Redux

<AuthRoute 
    exact 
    path="/settings/:itemId" 
    component={DoorsSettingsContainer} 
/> 

,并通过链接导航到它的第一次的时候:

<Link to={{ pathname: `/settings/${door._id}` }}> 
    <p className="sub-title-text-container">Inställningar</p> 
</Link> 

它工作正常,但是当我在DoorsSettingsContainer并刷新我的网页都崩溃。这是我的组件(我删除了我的进口以减少长度)。

// NOTE: There's no data here so my app crashes :-(
const getDoorById = (reduxStore, door) => { 
    return reduxStore.fetchDoors.doors.find(item => item._id == door) 
} 

const getControllerById = (reduxStore, controllerId) => { 
    return reduxStore.fetchDoors.controllers.find(
    item => item._id == controllerId 
) 
} 

class DoorSettingsContainer extends Component { 
    componentDidMount() { 
    this.props.fetchDoors() 
    } 

    render() { 
    const door = this.props.doors || [] 
    const controller = this.props.controllers || [] 
    if (this.props.isLoading) return <CircularProgress /> 
    return (
     <div> 
     <DoorSettingsForm 
      onSubmit={this.props.updateSettings} 
      door={door} 
      id={this.props.match.params} 
      controller={controller} 
     /> 
     </div> 
    ) 
    } 
} 

DoorSettingsContainer.propTypes = { 
    doors: PropTypes.object.isRequired, 
    controllers: PropTypes.object.isRequired, 
    fetchDoors: PropTypes.func.isRequired 
} 

const mapStateToProps = (state, ownProps) => { 
    const door = getDoorById(state, ownProps.match.params.itemId) 
    const doorId = ownProps.match.params.itemId 
    const controller = getControllerById(state, door.controller) 

    return { 
    doors: door, 
    controllers: controller, 
    isLoading: state.settings.isLoading 
    } 
} 

const mapDispatchToProps = dispatch => { 
    return { 
    fetchDoors:() => dispatch(fetchDoors()), 
    updateSettings: (id, door, controller) => 
     dispatch(updateSettings(id, door, controller)) 
    } 
} 

export default connect(mapStateToProps, mapDispatchToProps)(
    DoorSettingsContainer 
) 

这是我的错误信息: enter image description here 我想我应该提的是,我使用异步等待着我的行动fetchDoors,所以不正规的承诺。我也读过这篇文章:Why is componentDidMount not being called when I re-render?,但没有运气。

感谢您的阅读,希望我们可以一起解决这个问题。

+0

您需要调查为什么'''reduxStore.fetchDoors.doors'为'null'。插入'console.log(reduxStore.fetchDoors);'可能会提供线索。 –

+0

我试过你的类型,我唯一回来的是'isLoading:false',我猜这是因为我的'initialState'在我的reducer for'fetchDoors'中。 –

回答

2

你首先需要在您的对象执行nullcheck “reduxStore.fetchDoors.doors”

const getDoorById = (reduxStore, door) => { 
return (!!reduxStore && !!reduxStore.fetchDoors) ? reduxStore.fetchDoors.doors.find(item => item._id == door) : null 
} 

下一步是找出为什么你的对象“reduxStore.fetchDoors”是解决崩溃空。 如果我是你,我会先到你的Reducer并排除故障,如果你的商店状态被覆盖某处。

相关问题