【问题标题】:Uncaught type error: cannot read property persist of undefined with react-bootstrap carousel未捕获的类型错误:无法使用 react-bootstrap 轮播读取未定义的属性持久化
【发布时间】:2016-07-06 11:26:30
【问题描述】:

我正在使用 react-redux 实现 react-bootsrap 轮播,但标题中出现错误。

我使用的是受控轮播,当轮播自动更换幻灯片时出现错误消息。

当用户点击上一个。下一步按钮并手动更改它似乎没问题。

我不明白我应该将持久化作为道具或选项添加到道具或类似内容吗?

这是我的代码:

容器:

import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import store from 'store/configureStore'
import Slides from 'components/SlideShow'
import { getInitalSlides, handleSelect } from 'actions/SlidesActions'

class Home extends Component {

constructor(props) {
    super(props)
    this.state = {
        index: null,
        direction: null
    }
    this.handleSelect = this.handleSelect.bind(this)    

static fetchData({ store }) {
        return store.dispatch(getInitalSlides())
    }

    componentDidMount() {
        this.props.getInitalSlides()
    }

handleSelect(selectedIndex, e) {
        //alert(e)
        this.props.handleSelect(selectedIndex, e)
    }

  render() {
    return (
      <div className="Home">
        <h1>Home Page</h1>
        <Slides 
        slides={this.props.slides}   
        getInitialState={this.state.index} 
        getInitialStateD={this.state.direction}
        slidesControl={this.handleSelect}
        />
        <div><Link to="/question">to question</Link></div>
        <div><Link to="/posts">to posts</Link></div>
      </div>
    )
  }
}

function mapStateToProps (state) {
  const { slides, handleSelect } = state

  return { slides: state.slides, onSelect: state.handleSelect } 
}

export { Home }
export default connect(mapStateToProps { getInitalSlides, handleSelect})(Home)

这是组件中的相关位:

    render() {

    return (
      <Carousel 
      activeIndex={this.props.getInitialState} 
      direction={this.props.getInitialStateD} 
      onSelect={(selectedIndex,e)=>this.props.slidesControl(selectedIndex,e)}
      >
      {

      this.props.slides.map((s)=>{
              let id = s.get('id')
              let title = s.get('title')
              let image = s.get('image')
              let alt = s.get('alt')
              let caption = s.get('caption')
              return(

                    <Carousel.Item key={id} >
                    <img width={900} height={500} alt={s.get('alt')} src={image} alt={alt}/>
                    <Carousel.Caption>
                      <h3>{title}</h3>
                      <p>{caption}</p>
                    </Carousel.Caption>
                  </Carousel.Item>

              )
      }) 



    }
    </Carousel>)
  }
}

编辑: 这是相关的 react-bootstrap 轮播代码(抛出错误的地方)

  var onSelect = this.props.onSelect;

  if (onSelect) {
    if (onSelect.length > 1) {
      // React SyntheticEvents are pooled, so we need to remove this event
      // from the pool to add a custom property. To avoid unnecessarily
      // removing objects from the pool, only do this when the listener
      // actually wants the event.
      e.persist();
      e.direction = direction;

      onSelect(index, e);
    } else {
      onSelect(index);
    }
  }

【问题讨论】:

  • 你在handleSelect方法中使用事件参数e吗?
  • 你的意思是在动作创建器中?
  • export const HANDLE_SELECT = Symbol('HANDLE_SELECT') export function handleSelect(selectedIndex, e) { return { index: selectedIndex, direction: e.direction, //persist: e.persist, type: HANDLE_SELECT } }
  • 是的,在作为onSelect回调传递给Carousel的函数中。
  • 是的,正如您在上面的动作创建器中看到的那样,它被传递给我的 onSelect 我正在使用它来设置方向(e.direction)。不知道如何使用 e.persist 如果这确实是解决方案。

标签: javascript reactjs redux react-redux react-bootstrap


【解决方案1】:

我分析了react-bootstrapCarousel.js 代码,我怀疑这是react-bootstrap 库本身的问题。

There is this line triggering change in Carousel.js code:

this.timeout = setTimeout(this.next, this.props.interval);

this.next 方法需要SynteticEvent 类型的参数,但没有从setTimeout 调用传递。这解释了您的错误消息:...persist of undefined...

这个问题可能被隐藏了很长时间,but was exposed with this commit,其中事件参数实际上是由 Carousel.ja 代码本身使用的。

所以我建议创建 react-bootstrap Github 问题,同时降级到不包含上述提交的版本。

【讨论】:

  • 感谢您抽出宝贵的时间和麻烦。我会尝试发布一个 github 问题。
  • 我认为箭头函数绑定了它
  • 我删除了旁注
【解决方案2】:

这是项目 github 页面中的问题链接。似乎有一个修复即将到来:

https://github.com/react-bootstrap/react-bootstrap/issues/2029

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 2014-01-03
    • 2017-10-20
    • 2019-01-22
    • 2018-04-21
    • 2015-11-25
    相关资源
    最近更新 更多