2016-11-15 65 views
0

我有Redux的React Native App,我試圖從組件的構造函數中從Redux中獲取數據。 我得到的錯誤是這樣的: 可能未處理無極抑制(ID:0):不確定是不是(評估 'library.activeSections')可能未處理的承諾拒絕(id:0):undefined不是對象

這裏是我的代碼的對象:

class Accordion extends Component { 
... 

constructor (props) { 
super(props) 
this.state = { 
    activeSections: [this.props.initiallyActiveSection] 
} 

// if activeSection not specified, default to initiallyActiveSection 
let { library } = this.props 
if (library.activeSections) { //<-- here is an error library is undefined 
    this.setState({activeSections: library.activeSections}) 
} 

_toggleSection (section) { 
const activeSection = section 
    let isOpened = this.state.activeSections.indexOf(section) >= 0 ? true : false 
    var indexInActiveSections = this.state.activeSections.indexOf(section) 
if (isOpened === true) { 
     this.state.activeSections.splice(indexInActiveSections, 1) 
    } else { 
     this.state.activeSections.push(section) 
    } 

    this.props.libraryActions.setActiveSections(this.state.activeSections) 

    this.setState({ activeSection }) 
    } 

    render() { 
... 
} 

} 

    Accordion.PropTypes = { 
    library: React.PropTypes.arrayOf(React.PropTypes.number) 
} 

export default connect(state => ({ 
    library: state.library 
    }), 
    (dispatch) => ({ 
    libraryActions: bindActionCreators(libraryActions, dispatch) 
    }) 
)(Accordion) 

module.exports = Accordion 

這裏是圖書館減速機減速機/ library.js:

import { Map } from 'immutable' 

const initialState = Map({ 
    activeSections: [0] 
}) 

export default function library (state = initialState, action = {}) { 
    switch (action.type) { 
    case 'SET_ACTIVE_SECTIONS': 
     return { 
     ...state, 
     activeSections: action.activeSections 
     } 
    default: 
     return state 
    } 
} 

,這裏是圖書館的行動行動/ library.js:

export const setActiveSections = (activeSections) => { 
    return { 
    type: 'SET_ACTIVE_SECTIONS', 
    activeSections 
    } 
} 

這裏是減速/ index.js:

import articles from './articles' 
import journal from './journal' 
import library from './library' 

export { 
    articles, 
    journal, 
    library 
} 

我不明白爲什麼在this.props

希望對你有所幫助沒有庫對象。

+1

刪除行 'module.exports =手風琴',並嘗試 – Jickson

+0

它幫助!你是最棒的@Jickson – Dmitry

+0

很高興知道這對你有所幫助。添加它作爲答案,以便稍後幫助他人。 – Jickson

回答

1

刪除線module.exports = Accordion

相關問題