"Using React to implement dynamic variables"
P粉008829791
P粉008829791 2023-09-02 20:55:54
0
1
464
<p>I have the following code in 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>I tried to make the code dynamic like this: </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>Am I right? </p>
P粉008829791
P粉008829791

reply all(1)
P粉904405941

use:

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

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

This will map each element on your values ​​array and inject a new object in the Tabs array for each tab.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template