reactjs - 未调用反应状态更改 lambda 函数代码
问题描述
在 render() 函数中,我将一些代码存储在 const "success" 中。但是即使状态发生了变化,下面的 Card.Footer 标签之间对“成功”的引用也永远不会被调用。原因是什么?
render() {
let fields = this.props.fields.map(field => {
return(
<Form.Group controlId={field.col}>
<Form.Row>
<Form.Label>{field.label}</Form.Label>
<Form.Control required={field.required} type="text" placeholder={field.label} />
</Form.Row>
</Form.Group>
)
});
const success = () => {
console.log('B');
return(<small>Operation {this.props.operation} succeded.</small>);
if(this.state.dml_success) {
return(
<div>Operation {this.props.operation} succeded.</div>
)
}
};
console.log('C');
return(
<Card style={{ width: '18rem', backgroundColor: 'lightgrey' }}>
<Card.Body>
<Card.Title>{this.props.title}</Card.Title>
<Card.Text>
<Form onSubmit={this.dml.bind(this)}>
{fields}
<Form.Row>
<Button variant="primary" type="submit">
{this.props.operation}
</Button>
</Form.Row>
</Form>
</Card.Text>
</Card.Body>
<Card.Footer className="text-muted">{success}</Card.Footer>
</Card>
)
}
}
解决方案
您已将成功定义为函数,因此您需要添加括号
{success()}
推荐阅读
- java - Observable.just(doSomeLongStuff()) 在我订阅 observable 之前运行 doSomeLongStuff()
- shiny - 仅当按下操作按钮时 R 闪亮更新过滤器
- android - 离子找不到平台
- gradle - 如何使用 Gretty 插件在 Jetty 中运行我的 WAR?
- python - 用 Python 编写的函数,用于列出特定文件夹中的文件,而不过滤掉不需要的结果
- javascript - 如何根据我的网络中的单个字段进行多项搜索
- git - 如何仅列出用户在两次提交之间更改的文件名?
- fody-costura - 在 Fody.Costura 合并文件之前执行混淆
- swift - 如何通过 Swift 4 中的发件人检索 userData?
- html - 为什么图像标签不会像 html 中的任何文本块一样收缩?