"使用React实现动态变量"
P粉008829791
P粉008829791 2023-09-02 20:55:54
0
1
459
<p>我在React中有以下代码:</p> <pre class="brush:js;toolbar:false;">const TABS = [ { value: "Names", label: "Names", onclick: (obj) => { tabOnClick(obj.value); }, selected: mainTabSelected, }, { value: "Logs", label: "Logs", onclick: (obj) => { tabOnClick(obj.value); }, selected: mainTabSelected, }, { value: "Groups", label: "Groups", onclick: (obj) => { tabOnClick(obj.value); }, selected: mainTabSelected, }, { value: "Subscriptions", label: "Subscriptions", onclick: (obj) => { tabOnClick(obj.value); }, selected: mainTabSelected, }, ] </pre> <p>我试图将代码变为动态的,如下所示:</p> <pre class="brush:js;toolbar:false;">const values = ["Names","Logs","Groups","Subscriptions"]; const labels = ["Names","Logs","Groups","Subscriptions"]; const TABS = [ { value: {values}, label: {labels}, onclick: (obj) => { 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数组中注入新对象。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板