gpt4 book ai didi

javascript - 在课外访问 Prop

转载 作者:行者123 更新时间:2023-11-29 10:33:59 24 4
gpt4 key购买 nike

我正在使用启用了 cellEditreact-bootstrap-table。编辑单元格后,可以在 react-bootstrap-table 中使用回调函数。我将回调函数命名为 onAfterSaveCell。表中更新的行将保存到名为 row 的变量中。我想将 row 发送到一个名为 pushData 的 Action 创建者,它接受 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 但这给了我一个编译错误。我怎样才能使 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>......

关于javascript - 在课外访问 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39832839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com