【问题标题】:Possible Unhandled Promise Rejection (id:0): undefined is not an object可能未处理的 Promise Rejection (id:0): undefined is not an object
【发布时间】:2016-11-15 09:21:41
【问题描述】:

我有带有 Redux 的 React Native App,我正在尝试在组件的构造函数中从 Redux 获取数据。 我收到这样的错误: 可能未处理的 Promise Rejection (id:0): undefined is not an object (evalating 'library.activeSections')

这是我的代码:

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

这里是库减速器 reducers/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
  }
}

这里是 reducers/index.js:

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

export {
  articles,
  journal,
  library
}

我不明白为什么this.props中没有库对象

希望得到您的帮助。

【问题讨论】:

  • 删除'module.exports = Accordion'行并尝试
  • 它有帮助!你是最棒的@Jickson
  • 很高兴知道它对您有所帮助。将其添加为答案,以便以后对某人有所帮助。

标签: javascript react-native redux react-redux


【解决方案1】:

删除module.exports = Accordion这一行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    相关资源
    最近更新 更多