Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我可以设置尚未定义的OnClick事件吗?_Javascript_Reactjs - Fatal编程技术网

Javascript 我可以设置尚未定义的OnClick事件吗?

Javascript 我可以设置尚未定义的OnClick事件吗?,javascript,reactjs,Javascript,Reactjs,我有一个组件如下所示: const ProductCarousel = React.createClass({ componentDidMount: function () { this.flky = new Flickity('.carousel', flickityOptions) //here }, render: function () { const item = this.props.item return ( <div&

我有一个组件如下所示:

const ProductCarousel = React.createClass({
  componentDidMount: function () {
    this.flky = new Flickity('.carousel', flickityOptions)
    //here
  },
  render: function () {
    const item = this.props.item
    return (
      <div>
        <div className='carousel'>
          {item.get('images').map((url, i) => (
            <img key={i.toString()} src={stripUrl(url)} width={520} />
          ))}
        </div>
        <div style={{marginTop: '20px', display: 'flex'}}>
          {item.get('images').map((url, index) =>
            <div style={{flexGrow: 1, margin: '0 1em 0 1em'}} className='hidden-xs' key={url}>
              <Thumbnail src={stripUrl(url)} />
            </div>
          )}
        </div>
      </div>
    )
  }
})
const ProductCarousel=React.createClass({
componentDidMount:函数(){
this.flky=新的Flickity('.carousel',flickityOptions)
//这里
},
渲染:函数(){
const item=this.props.item
返回(
{item.get('images').map((url,i)=>(
))}
{item.get('images').map((url,索引)=>
)}
)
}
})

在这里,我想为
定义函数。这些函数是用于
this.flky
的方法,因此我无法在
componentDidMount
之前创建它们,但我想在缩略图中设置它们并将缩略图索引传递给它们。是否有类似于承诺的东西我可以使用?

如果你把它放在一个函数中,它在被点击之前不会被执行,而这些方法应该在那时就存在了:

 <Thumbnail src={stripUrl(url)} onClick={()=>this.flky.someMethod(index)} />
this.flky.someMethod(index)}/>

如果将其放入函数中,则在单击之前不会执行该函数,并且该方法应在该函数中存在:

 <Thumbnail src={stripUrl(url)} onClick={()=>this.flky.someMethod(index)} />
this.flky.someMethod(index)}/>

Cool,我必须像那样将方法内联还是可以进行外部引用?只要onclick函数在渲染时存在,你应该能够将其外部化。Cool,我必须像那样将方法内联还是可以进行外部引用?你应该能够将其外部化,只要onclick函数在渲染时存在。