Reactjs 未调用反应状态更改lambda功能代码

Reactjs 未调用反应状态更改lambda功能代码,reactjs,react-bootstrap,Reactjs,React Bootstrap,在render函数中,我在constsuccess中存储了一些代码。但即使状态改变,下面的Card.Footer标记之间对success的引用也永远不会被调用。 原因是什么 render() { let fields = this.props.fields.map(field => { return( <Form.Group controlId={field.col}> <Form.Row> <Form.Lab

在render函数中,我在constsuccess中存储了一些代码。但即使状态改变,下面的Card.Footer标记之间对success的引用也永远不会被调用。 原因是什么

 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()}

您需要调用函数{success},而不是{success}。

尝试{success},或者在success函数中接受道具。编译组件时可能也会出现linting错误,因为您有一个返回,并且下面的代码永远不会执行,如果不满足任何条件,则至少应该返回null。