簡體   English   中英

在 React 中跨項目拆分功能?

[英]Split up functionalities across project in React?

好吧,所以我是 React 的新手,並且制作了一個應用程序,它從 API 獲取一些數據並將其存儲到一個表中。 它有效,但大部分代碼都在一個文件中,這讓我很惱火。 現在我正在嘗試將它拆分為多個文件,但到目前為止還沒有太多運氣。

我按照一個答案說我可以創建一個APIManager並使用以下代碼:

import React from 'react';

const base_url = 'https://europe-west1-single-router-309308.cloudfunctions.net/GET?';

export let buildURL = (offset, pagination, product_name, store_name) => {
    [...]
}

但是,這給了我一個TypeError: Object(...) is not a function 我用普通的 function 嘗試了它,導出 function 和這個,但沒有任何效果。 我應該用它做一個組件嗎? 這是代碼被調用的地方:

<div className='header'>
          <Form handleSubmit={(offset, pagination, product_name, store_name) => {buildURL(offset, pagination, product_name, store_name)}}/>
</div>

好吧,在@digitalbreed 的幫助下,我設法修復了它! 我變了

import buildURL from './src/managers/APIManager';

import * as APIManager from './src/managers/APIManager';

並更改了我對代碼的引用

<Form handleSubmit={(offset, pagination, product_name, store_name) => { APIManager.buildURL(offset, pagination, product_name, store_name)}}/>

<Form handleSubmit={() => this.handleSubmit(offset, pagination, product_name, store_name)}/>

handleSubmit(offset, pagination, product_name, store_name){
  var url = APIManager.buildURL(offset, pagination, product_name, store_name);

現在它工作正常。 感謝您的幫助,數字品種!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM