Javascript 对象作为子错误无效。找到具有键()的对象

Javascript 对象作为子错误无效。找到具有键()的对象,javascript,reactjs,react-native,ecmascript-6,Javascript,Reactjs,React Native,Ecmascript 6,我正在尝试编写一个动态渲染按钮的方法,并试图通过使用renderButton方法避免代码重复。我在任何地方都找不到解决这个问题的可靠办法,有人能帮忙吗?我还没有反应过来 导出默认类DialogBoxStory扩展PureComponent{ 状态={isVisible:false,键入:“确认”} 隐藏(类型){ 返回(有效载荷=“”)=>{ this.setState({isVisible:false}) 调试(`hide${type}`,有效负载) } } 显示(类型){ 返回(有效载荷)

我正在尝试编写一个动态渲染按钮的方法,并试图通过使用renderButton方法避免代码重复。我在任何地方都找不到解决这个问题的可靠办法,有人能帮忙吗?我还没有反应过来

导出默认类DialogBoxStory扩展PureComponent{
状态={isVisible:false,键入:“确认”}
隐藏(类型){
返回(有效载荷=“”)=>{
this.setState({isVisible:false})
调试(`hide${type}`,有效负载)
}
}
显示(类型){
返回(有效载荷)=>{
this.setState({isVisible:true,type})
调试(`show${type}`,有效负载)
}
}
渲染按钮(){
返回[‘确认’、‘警报’、‘输入’]
.map((类型)=>(
))
}
渲染(){
常量选项={
标题:文本(“标题”、“测试标题”、“道具”),
消息:文本('message',lorem',props'),
onOkPress:this.hide('onOkPress'),
onCancelPress:this.hide('onCancelPress'),
isVisible:this.state.isVisible,
类型:this.state.type,
onRequestClose:this.hide('onRequestClose'),
}
返回(
{this.renderButtons}
)
}
}
  • 将所有方法(函数)声明转换为箭头函数,如
    show=()=>{your function Body}

  • 您必须调用renderButtons函数
    {this.renderButtons()}

  • 这部分代码
    onPress={this.show(type)}
    必须使用
    bind()
    方法,并将
    this
    关键字作为第一个参数
    onPress={this.show.bind(this,type)}


应该是
{this.renderButtons()}
不!仍然失败。这与我返回按钮的方式有关删除此行后您是否尝试过:
{this.renderButtons()}
?之后还能用吗?是的!它仍然不起作用。您可以尝试在此处添加
return
<代码>返回
   export default class DialogBoxStory extends PureComponent {
  state = { isVisible: false, type: 'confirm' }

  hide (type) {
    return (payload = '') => {
      this.setState({ isVisible: false })
      console.debug(`hide ${type}`, payload)
    }
  }

  show (type) {
    return (payload) => {
      this.setState({ isVisible: true, type })
      console.debug(`show ${type}`, payload)
    }
  }

  renderButtons () {
    return [ 'confirm', 'alert', 'input' ]
      .map((type) => (
        <Button
          title={`show ${type} dialog`}
          type="primary"
          onPress={this.show(type)}
        />
      ))
  }


  render () {
    const options = {
      title: text('title', 'Test Title', 'props'),
      message: text('message', lorem, 'props'),
      onOkPress: this.hide('onOkPress'),
      onCancelPress: this.hide('onCancelPress'),
      isVisible: this.state.isVisible,
      type: this.state.type,
      onRequestClose: this.hide('onRequestClose'),
    }

    return (
      <Fragment>
        <DialogBox {...options} />
        {this.renderButtons}
      </Fragment>
    )
  }
}