1

Using React.js and Semantic UI React I'd like to dynamically create tabs.

When using a const representing the array of tab objects, this works fine.

const panes = [
            {
                menuItem: 'Record 1',
                render: () => <Tab.Pane>Tab 1 Content</Tab.Pane>
            }
        ]
...
<Tab panes={panes}/>

However, I need these to be dynamically added based on the state variable source:

getPanes() {
        return this
            .state
            .source
            .map((source) => [{
                menuItem: source.sourceName,
                render: () => <Tab.Pane>Tab 1 Content</Tab.Pane>
            }])
    }
...
<Tab panes={this.getPanes}/>

This results in:

Warning: Failed prop type: Invalid prop `panes` of type `function` supplied to `Tab`, expected an array.

1 Answer 1

1

Two issues:

  1. this.getPanes refers to your function definition, it doesn't invoke your function. You want the result of your function, should you should invoke it using parentheses: this.getPanes()
  2. .map() returns a new array of elements that is the result of applying your supplied function to each original element. What your really want to return within .map is your pane object, not an array of one pane object:

--

.map((source) => ({ 
  menuItem: source.sourceName, 
  render: () => <Tab.Pane>Tab 1 Content</Tab.Pane> 
}))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.