如何使React Native中可重复使用的代码?

发布于 2025-02-14 01:50:22 字数 667 浏览 0 评论 0原文

如何创建可重复使用的“函数”:

const Customer = (name, ID) => {
<Text style={{ fontSize: 12 }}>{name}</Text>
<Text style={{ fontSize: 10 }}>{ID}</Text>}

这样我可以轻松地将客户插入以下功能。

function Shop() {
return (
    <DraxProvider>

        <DraxView>
            <Customer name="A" ID="1">
        </DraxView>

        <DraxView>
            <Customer name="B" ID="2">
        </DraxView>

        <DraxView>
            <Customer name="C" ID="3">
        </DraxView>

    </DraxProvider>
)}

blockquote

How can I create reusable "functions" like this:

const Customer = (name, ID) => {
<Text style={{ fontSize: 12 }}>{name}</Text>
<Text style={{ fontSize: 10 }}>{ID}</Text>}

So that I can insert customers into the following function easily.

function Shop() {
return (
    <DraxProvider>

        <DraxView>
            <Customer name="A" ID="1">
        </DraxView>

        <DraxView>
            <Customer name="B" ID="2">
        </DraxView>

        <DraxView>
            <Customer name="C" ID="3">
        </DraxView>

    </DraxProvider>
)}

Blockquote

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2025-02-21 01:50:22

您应该首先尝试将您的customer作为一个类似的组件,

const Customer = ({name, ID}) => (
<>
  <Text style={{ fontSize: 12 }}>{name}</Text>
  <Text style={{ fontSize: 10 }}>{ID}</Text>
</>
)

请注意以下注意:

  • 箭头符号后,您应该使用符号,而不是{ {
  • 使用props将参数传递到
  • &lt;&lt;&lt;/&gt;/&gt; aka fragments的组件用法文本组件

您应该能够按照预期重复使用

function Shop() {
return (
    <DraxProvider>

        <DraxView>
            <Customer name="A" ID="1">
        </DraxView>

        <DraxView>
            <Customer name="B" ID="2">
        </DraxView>

        <DraxView>
            <Customer name="C" ID="3">
        </DraxView>

    </DraxProvider>
)}

You should first try to make your Customer as a component like below

const Customer = ({name, ID}) => (
<>
  <Text style={{ fontSize: 12 }}>{name}</Text>
  <Text style={{ fontSize: 10 }}>{ID}</Text>
</>
)

Note the following:

  • after the arrow sign you should use ( sign as opposed to {
  • Usage of props to pass parameter to component
  • Usage of <></> aka Fragments since you have two Text component

With that you should be able to reuse as you intended

function Shop() {
return (
    <DraxProvider>

        <DraxView>
            <Customer name="A" ID="1">
        </DraxView>

        <DraxView>
            <Customer name="B" ID="2">
        </DraxView>

        <DraxView>
            <Customer name="C" ID="3">
        </DraxView>

    </DraxProvider>
)}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文