繁体   English   中英

在课外访问道具

[英]Access props outside class

我正在使用启用了cellEdit react-bootstrap-table 编辑单元格后,可以在react-bootstrap-table使用回调函数。 我将回调函数命名为onAfterSaveCell table 中更新的行将保存到名为row的变量中。 我想将row发送给一个名为pushData的动作创建者,它接受row作为输入。 我的代码看起来像这样。

function onAfterSaveCell(row, cellName, cellValue){
  this.props.pushData(row);
} 

const cellEditProp = {
  mode: "click",
  blurToSave: true,
  afterSaveCell: onAfterSaveCell
};

class Feature extends Component {
  componentWillMount() {
    this.props.fetchMessage();
  }

  render() {
    if (!this.props.message) return null

    return (
    <div>
      <BootstrapTable
              data={this.props.message}
              cellEdit={cellEditProp}
              >
        <TableHeaderColumn dataField="ccyCode" isKey={true} dataSort={true}>Valutakod</TableHeaderColumn>......

当我运行代码时,出现错误Uncaught TypeError: Cannot read property 'props' of undefined 我认为这是因为propsclass Feature之外不可用。 我试图将onAfterSaveCell函数和cellEditProp移到class Feature但这给了我一个编译错误。 如何让propsclass Feature之外可访问,或者应该以其他方式解决?

你是对的,你不能在课堂外使用this关键字。 所以显而易见的答案是在类中定义你的回调函数。 我认为您遇到编译错误的原因是语法错误,因为这应该可以正常工作:

class Feature extends Component {
  constructor(props, context) {
    super(props, context);
    this.onAfterSaveCell = this.onAfterSaveCell.bind(this); // important!
  }

  componentWillMount() {
    this.props.fetchMessage();
  }

  onAfterSaveCell(row, cellName, cellValue) {
    this.props.pushData(row);
  }



  render() {
    if (!this.props.message) return null

    return (
    <div>
      <BootstrapTable
              data={this.props.message}
              cellEdit={{
                mode: "click",
                blurToSave: true,
                afterSaveCell: this.onAfterSaveCell
                }}
              >
        <TableHeaderColumn dataField="ccyCode" isKey={true} dataSort={true}>Valutakod</TableHeaderColumn>......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM