如何从反应函数调用类(how to call a class from the function i

2019-10-30 04:30发布

基本上,我试图调用从轴响应模态类。 我想显示模式弹出,而不是在下面的代码警报。 谁能帮助如何调用从爱可信拦截功能的模态类? 提前致谢

import React, { Component}  from 'react';
import axios from 'axios';
import Modals  from '../components/modalAlerts/modalalerts'
    const instance = axios.create({
        baseURL: 'http://someurl',
        timeout: 15000,
    }); 

    instance.defaults.headers.common['Authorization'] = //sequrity token will be here
    instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

    instance.interceptors.request.use(function (config) {

        return config;
      }, function (error) {

        alert(error)
        return Promise.reject(error);
      });
    instance.interceptors.response.use(function (config) {

        return config;
      }, function (error) {
        console.log(error)
        if(error.response){
            if(error.response.status === 401||error.response.status === 403 ){
                localStorage.clear()
                alert(error.response.data.message)
            }else if(error.response.status === 404){
                console.log("404")
            }else if(error.response.status === 400){
               alert(error.response.data.message)
            }else{
                alert("something went wrong. Please try after sometime..!")
            }
        }else{
            alert("server not found")
        }

        return Promise.reject(error);
      });
    export default instance;

在这里我modalalerts.js文件是有在渲染以后我会改变它的一些变化(如多增加一些领域,如状态有多个过滤器)

    import React, { Component } from 'react';
import { Button, Card, CardBody, CardHeader, Col, Modal, ModalBody, ModalFooter, ModalHeader, Row } from 'reactstrap';

class Modals extends Component {
    constructor(props) {
        super(props);
        this.state = {
            modal: true,
        }    
        this.toggle = this.toggle.bind(this);
    }

    toggle() {
        this.setState({
            modal: !this.state.modal,
        });
    }

    render() {
        return (
            <div className="animated fadeIn">
                <Row>
                    <Col>
                        <Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
                            <ModalHeader toggle={this.toggle}>{this.props.apistatus == 1? "Success" : "Error"}</ModalHeader>
                            <ModalBody>
                                {this.props.apistatus == 1? "Form updated successfully" : this.props.errormsg}
                            </ModalBody>
                            <ModalFooter>
                                <Button color="primary" onClick={this.toggle}>OK</Button>{' '}
                                {/* <Button color="secondary" onClick={this.toggle}>Cancel</Button> */}
                            </ModalFooter>
                        </Modal>       
                    </Col>
                </Row>
            </div>
        );
    }
}

export default Modals;
文章来源: how to call a class from the function in react