[英]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
。 我认为这是因为props
在class Feature
之外不可用。 我试图将onAfterSaveCell
函数和cellEditProp
移到class Feature
但这给了我一个编译错误。 如何让props
在class 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.