在另一个类中调用一个类方法

时间:2019-02-07 13:33:07

标签: javascript reactjs crud

我有返回类`<script type="text/javascript"> var validNavigation = false; function bindDOMEvents() { $(document).click("a", function () { validNavigation = true; }); $(document).on("submit", "form", function () { validNavigation = true; }); $(document).bind("click", "input[type=submit]", function () { validNavigation = true; }); $(document).bind("click", "button[type=submit]", function () { validNavigation = true; }); $(document).bind("click", "radio[type=submit]", function () { validNavigation = true; }); $(document).bind("click", "checkbox[type=submit]", function () { validNavigation = true; }); $(window).keydown(function (e) { validNavigation = e.which === 116 ? true : false }); } function callServerForBrowserCloseEvent() { $.ajax({ type: "GET", url: "@Url.Action("LogOut", "Account")", contentType: "application/json; charset=utf-8", dataType: 'JSON', traditional: true, async: false }); } $(document).ready(function () { bindDOMEvents(); window.addEventListener("beforeunload", function () { if (!validNavigation) callServerForBrowserCloseEvent(); else validNavigation = false; }); }); </script>` 的服务函数withFetching

Fetch

我需要另一个类中的调用方法import React from 'react'; import * as axios from 'axios'; const withFetching = url => Component => { class Fetch extends React.Component { state = { data: null, isLoading: false }; componentDidMount() { this.setState({isLoading: true}); } get = (params) => { axios.get(url, params) .then(response => { this.setState({data: response.data, isLoading: false}); }) .catch(error => { console.error('%c ERROR', 'color: #FA12D6', error); }); }; post = data => {} put = () => {} render() { return <Component {...this.state} {...this.props} post={this.post} put={this.put} delete={this.delete}/>; } } Fetch.displayName = `Fetch(${Component.displayName || Component.name || 'Component'})`; return Fetch; }; export default withFetching; 。我正在做类似的事情:

get

1 个答案:

答案 0 :(得分:0)

我解决了调用CRUD服务方法的问题

import React from 'react';
import WithFetching from './../../../../services/withFetching';

class MyComponent extends React.Component {
    state = {};

    componentWillMount() {
        const {post} = this.props;
        post({data:'data'});
    }

    render() {
        return (
            ...
        );
    }
}

export default WithFetching('URL')(MyComponent);

我们通过post获得了类方法props,并在组件(componentWillMount)的生命周期中对其进行了调用