"使用React实现动态变量"
P粉008829791
P粉008829791 2023-09-02 20:55:54
[React讨论组]
<p>我在React中有以下代码:</p> <pre class="brush:js;toolbar:false;">const TABS = [ { value: &quot;Names&quot;, label: &quot;Names&quot;, onclick: (obj) =&gt; { tabOnClick(obj.value); }, selected: mainTabSelected, }, { value: &quot;Logs&quot;, label: &quot;Logs&quot;, onclick: (obj) =&gt; { tabOnClick(obj.value); }, selected: mainTabSelected, }, { value: &quot;Groups&quot;, label: &quot;Groups&quot;, onclick: (obj) =&gt; { tabOnClick(obj.value); }, selected: mainTabSelected, }, { value: &quot;Subscriptions&quot;, label: &quot;Subscriptions&quot;, onclick: (obj) =&gt; { tabOnClick(obj.value); }, selected: mainTabSelected, }, ] </pre> <p>我试图将代码变为动态的,如下所示:</p> <pre class="brush:js;toolbar:false;">const values = [&quot;Names&quot;,&quot;Logs&quot;,&quot;Groups&quot;,&quot;Subscriptions&quot;]; const labels = [&quot;Names&quot;,&quot;Logs&quot;,&quot;Groups&quot;,&quot;Subscriptions&quot;]; const TABS = [ { value: {values}, label: {labels}, onclick: (obj) =&gt; { tabOnClick(obj.value); }, selected: mainTabSelected, }] </pre> <p>我是对的吗?</p>
P粉008829791
P粉008829791

全部回复(1)
P粉904405941

使用:

const values = ["Names", "Logs", "Groups", "Subscriptions"];

const TABS = values.map((e, i) =>
    {
        return {
            value: e,
            label: e,
            onclick: (obj) => {
                tabOnClick(obj.value);
            },
            selected: mainTabSelected,
        }
    }
)

这将在您的values数组上映射每个元素,并为每个选项卡在Tabs数组中注入新对象。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号