简体   繁体   English

如何重用一块 HTML 作为组件并将其传递给多个组件?

[英]How can I reuse a piece of HTML as a component and pass it to multiple components?

I want to ask for a way to create a component, and from that Component, I can use it as a child component for multiple other Components to use.我想寻求一种创建组件的方法,并且从该组件中,我可以将其用作子组件,以供多个其他组件使用。

In my case:就我而言:

I have set up a route like this in App.js , it's very simple我在App.js中设置了这样的路由,很简单

        <Routes>
            <Route exact path="/" element={<Newest />} />
            <Route path="/movies" element={<Movies />} />
            <Route path="/tv" element={<TvShows />} />
            <Route path="/series" element={<Series />} />
            <Route path="/cartoons" element={<Cartoons />} />
        </Routes>

From then, I create 5 other components: Newest , Movies , TvShows , Series , Cartoon从那时起,我创建了 5 个其他组件: NewestMoviesTvShowsSeriesCartoon

As I see from my code, there's a piece of JSX that I've always used, for example正如我从我的代码中看到的,有一段我一直使用的 JSX,例如

This is my Newest component:这是我Newest的组件:

import { useState, useEffect } from "react"
import axios from "axios"
import { SITE_API } from "../../constants/constants"

function Newest() {
    const [newestData, setNewestData] = useState([])
    const newestUrl = `${SITE_API}/update-film`

    useEffect(() => {
        const CancelToken = axios.CancelToken
        const source = CancelToken.source()

        axios
            .get(newestUrl, { cancelToken: source.token })
            .then((data) => {
                setNewestData(data.data.data.items)
            })
            .catch((thrown) => {
                if (axios.isCancel(thrown)) return
            })
    }, [])

    return (
        <>
            <h2>NEWEST</h2>
            <div className="holder-item">
                {newestData.map((item, i) => {
                    return (
                        <div className="item" key={i}>
                            <img
                                src={`shorten-URL-that-I-use{item.thumb_url}`}
                                alt=""
                            />
                            <h3>{item.name}</h3>
                        </div>
                    )
                })}
            </div>
        </>
    )
}

export default Newest

There's a whole "holder-item" section that I can reuse for 4 Components left.有一个完整的"holder-item"部分,我可以重复使用剩下的 4 个组件。

I tried to create a component called ItemCard and paste that whole section inside of it, it's like this:我尝试创建一个名为ItemCard的组件并将整个部分粘贴到其中,就像这样:

import React from "react"

function ItemCard({ newestData }) {
    console.log(newestData)

    return (
        <>
            {newestData.map((item, i) => {
                return (
                    <div className="item" key={i}>
                        <img
                            src={`shorten-URL-that-I-use${item.thumb_url}`}
                            alt=""
                        />
                        <h3>{item.name}</h3>
                    </div>
                )
            })}
        </>
    )
}

export default ItemCard

And from that, back to Newest component, I changed it to:然后,回到Newest组件,我将其更改为:

import { useState, useEffect } from "react"
import axios from "axios"
import { SITE_API } from "../../constants/constants"

import ItemCard from "../ItemCard"

function Newest() {
    const [newestData, setNewestData] = useState([])
    const newestUrl = `${SITE_API}/update-film`

    useEffect(() => {
        const CancelToken = axios.CancelToken
        const source = CancelToken.source()

        axios
            .get(newestUrl, { cancelToken: source.token })
            .then((data) => {
                setNewestData(data.data.data.items)
            })
            .catch((thrown) => {
                if (axios.isCancel(thrown)) return
            })
    }, [])

    return (
        <>
            <h2>NEWEST</h2>
            <div className="holder-item">
                <ItemCard newestData={newestData} />
            </div>
        </>
    )
}

export default Newest

So, is there a way for me to use the ItemCard component for 4 other components left?那么,我有没有办法将ItemCard组件用于剩下的 4 个其他组件? Because I think I need to pass 4 other props to ItemCard , but if so, how can I use it for each route, it's my first time trying this method, so please instruct me, I don't know what's this situation call to google it myself, thanks.因为我认为我需要将其他 4 个 props 传递给ItemCard ,但是如果是这样,我该如何为每条路线使用它,这是我第一次尝试这种方法,所以请指导我,我不知道这种情况打电话给谷歌我自己,谢谢。

You can use it like you've already used it here, but you will either have to pass "newestData" as a prop, assuming it has a children/parent relationship.您可以像在这里使用它一样使用它,但是您必须将“newestData”作为道具传递,假设它具有子/父关系。 If it doesn't, you can use a common library called redux, with which you can set a global state of the newestData and use it anywhere in your code.如果没有,您可以使用名为 redux 的通用库,您可以使用该库设置最新数据的全局 state 并在代码中的任何位置使用它。 If you do not want to use another library, you can accomplish the same using the built in react hook useContext.如果你不想使用另一个库,你可以使用内置的 react 钩子 useContext 来完成同样的事情。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM