javascript - 如何使用通用方法通过重用代码来定义 React 组件

标签 javascript reactjs api single-page-application

我目前有几个在功能方面看起来相似的 React 组件。它们都有相似的方法,用于从服务器获取数据并通过为不同端点创建 API 来更新每个组件的状态。

import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
import AppService from '../lib/service';

class Factorial extends Component {

    constructor(props) {
        super(props);

        this.state = {
            value: '',
        }
    }

    getNext() {
        AppService.getNextFactorial().then(data => {
            this.setState({ value: data.data.gen })
        })
    }

    resetFactorial() {
        AppService.resetNextFactorial().then(data => {
            this.setState({ value: data.data.reset })
        })
    }

    render() {
        return (
            <div className={'functionsWrapper'}>
                <h2> Factorial Sequence Generator </h2>
                <p> Click to get the next value in the sequence </p>
                <button onClick={() => this.getNext()}> Get Next</button>
                <button onClick={() => this.resetFactorial()}> Reset </button>

                <div>
                    <input type="text" id="body" defaultValue={this.state.value} name="body" className="form-input" />
                </div>
            </div>
        )
    }
}

export default withRouter(Factorial);
import React, { Component } from 'react';
import AppService from '../lib/service';

class Fibonacci extends Component {

    constructor(props) {
        super(props);

        this.state = {
            value: '',
        }
    }

    getNext() {
        AppService.getNextFibonacci().then(data => {
            this.setState({ value: data.data.gen })
        })
    }

    resetFibonacci() {
        AppService.resetNextFibonacci().then(data => {
            this.setState({ value: data.data.reset })
        })
    }

    render() {
        return (
            <div className={'functionsWrapper'}>
                <h2> Fibonacci Sequence Generator </h2>
                <p> Click to get the next value in the sequence </p>
                <button onClick={() => this.getNext()}> Get Next</button>
                <button onClick={() => this.resetFibonacci()}> Reset </button>

                <div>
                    <input type="text" id="body" defaultValue={this.state.value} name="body" className="form-input" />
                </div>
            </div>
        )
    }
}

export default Fibonacci;

我想要实现的是能够拆分每个组件的功能并使其可重用,以便我可以拥有通用的方法

最佳答案

helper.js

import AppService from '../lib/service';

export function resetFibonacci() {
    AppService.resetNextFibonacci().then(data => {
        this.setState({ value: data.data.reset })
    });    
}

NOTE: use function insted of an arrow function "() => {}"

Fibonacci.js(您的组件)

import * as Helpers from './helpers.js';
...
resetFibonacci() {
  Helpers.resetFibonacci.call(this);
}

NOTE: bind this to the function scope.

关于javascript - 如何使用通用方法通过重用代码来定义 React 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54165930/

相关文章:

javascript - 有没有办法在 Angularjs 的指令中获取子元素的宽度和高度

javascript - 将 applescript 变量传递给 Javascript

reactjs - 您应该在每个 `test()/it()` block 中还是在全局中渲染组件/选择元素?

JavaScript 对象数组包含相同的数组数据

javascript - 可以使用useRef钩子(Hook)清空输入值吗?

reactjs - 如何修复绑定(bind)元素 'children' 隐式具有 'any' type.ts(7031)?

reactjs - 访问react组件的根元素

api - 使用 prestashop api 需要用户登录 web 服务

c++ - 为什么模块仍然可以访问在外部模块外部实现的方法

javascript - 使用 Bit.ly API 仅使用 Javascript 来缩小 URL