简体   繁体   English

将元素从子组件移动到其父组件

[英]Move element from child component to its parent

I want to design an ApiFetcher in order to prevent duplicate code.我想设计一个 ApiFetcher 以防止重复代码。

How to I pass a custom child component to my ApiFetcher so that it renders the content I specify in the custom component instead of a hard coded element in the return statement?如何将自定义子组件传递给我的 ApiFetcher,以便它呈现我在自定义组件中指定的内容,而不是返回语句中的硬编码元素?

I want to keep the logic of the ApiFetcher and the CompanyProfile components out of their parent components.我想将 ApiFetcher 和 CompanyProfile 组件的逻辑保留在其父组件之外。

import React from "react";
import ReactDOM from "react-dom";
import { useState, useEffect } from "react";

function ApiFetcher(props) {
  const apiUrl =
    "https://financialmodelingprep.com/api/v3/profile/AAPL?apikey=demo";

  const [error, setError] = useState(null);
  const [isLoaded, setIsLoaded] = useState(false);
  const [items, setItems] = useState([]);

  useEffect(() => {
    fetch(apiUrl)
      .then((res) => res.json())
      .then(
        (result) => {
          setIsLoaded(true);
          setItems(result);
        },
        (error) => {
          setIsLoaded(true);
          setError(error);
        }
      );
  }, []);

  if (error) {
    return <div>Error: {error.message}</div>;
  } else if (!isLoaded) {
    return <div>Loading...</div>;
  } else {
    // TODO: move the following <div> out of this component and render children
    return (
      <div>
        {items.map((item) => (
          <li key={item.symbol}>
            {item.companyName} {item.price}
          </li>
        ))}
      </div>
    );
  }
}

function CompanyProfile() {
  return (
    <div>
      <ApiFetcher>
        {/*
        TODO: move the following into this component:

      <div>
        {items.map((item) => (
          <li key={item.symbol}>
            {item.companyName} {item.price}
          </li>
        ))}
      </div>
    */}
      </ApiFetcher>
    </div>
  );
}

ReactDOM.render(<CompanyProfile />, document.getElementById("root"));

You can maybe use props.children in order to move rendering logic to Parent component.您可以使用props.children将渲染逻辑移动到父组件。 Here's how I will strucutre the component:以下是我将如何构建组件:

function ApiFetcher({url}) {
  const [error, setError] = useState(null);
  const [isLoaded, setIsLoaded] = useState(false);
  const [items, setItems] = useState([]);

  useEffect(() => {
    fetch(url)
      .then((res) => res.json())
      .then(
        (result) => {
          setIsLoaded(true);
          setItems(result);
        },
        (error) => {
          setIsLoaded(true);
          setError(error);
        }
      );
  }, []);

  if (error) {
    return <div>Error: {error.message}</div>;
  }

  if (!isLoaded) {
    return <div>Loading...</div>;
  }

  return props.children({response: items});
}

Usage用法

<ApiFetcher>
{({response}) => // Rendering logic}
</ApiFetcher>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React 中将 dom 元素从子组件移动到父组件? - How to move a dom element from a child component to a parent component in React? 是否可以将元素 ref 从子功能组件传递到其父功能组件? - Is it possible to pass a element ref from a child functional component to its parent functional component? VueJS如何从子组件向其父组件发出事件 - VueJS How to emit an event from a child component to its parent component 在层次结构中将子元素移动到父元素 - Move child element to parent in hierarchy jQuery 将子元素的子节元素移动到其父元素并移除其他元素 - jQuery move child's child section element to its parent and remove other elements ReactJS-将数据从子组件传递到其父组件 - ReactJS - passing data from a child component to its parent Svelte - 将变量从父组件传递给其所有子组件 - Svelte - pass variable from parent component to all its child components ReactJS 类 如何将 State 从子组件传递给其父组件? - ReactJS Classes How to pass State from Child Component to its Parent? 从其子项触发 React 父功能组件中的 function - Triggering a function in a React parent functional component from its child 如何根据其子组件状态更改父组件样式元素? - How to change parent-component style element according to its child-component state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM