0

How to return different component using map function. How to apply condition inside of map function

I am trying to return a component if subMain is true then return that part of component and if subMain is false then return that part of component.

But here the error in the structure only . the error shows here !subMain

Update: I tried another way but this throws error in second return and in const MainPath

            {states &&            
            states.map(({ Main }) => (
                Main.map(({component , subMain}) => (
                    const MainPath = `/${component}`
                    const MainComponent = `${component}`
                    subMain ? subMain.map(({ component }) => {
                        
                            const Path = `/${component}`
                            const Component = `${component}`
                            return (
                                <Protected
                                path={Path}
                                component={Component}
                                exact
                            />
                            )
                        }):
                            return (
                                <Protected
                                path={MenuPath}
                                component={MenuComponent}
                                exact
                            />
                            )
                                                    
                ))
            ))
        }
1
  • your subMain is not boolean! Commented Sep 11, 2020 at 17:12

1 Answer 1

1

Edit : After question was updated i think there were just some syntax errors and missing return statement

You're running 2 expressions instead of just one return value. You should use a ternary like so

  {states &&            
        states.map(({ Main }) => (
            Main.map(({component , subMain}) => {
                const MainPath = `/${component}`
                const MainComponent = `${component}`
                return subMain ? subMain.map(({ component }) => {
                    
                        const Path = `/${component}`
                        const Component = `${component}`
                        return (
                            <Protected
                            path={Path}
                            component={Component}
                            exact
                        />
                        )
                    }):
                  (
                      <Protected
                         path={MenuPath}
                         component={MenuComponent}
                         exact
                      />
                  )
                                                
            })
        ))
    }
Sign up to request clarification or add additional context in comments.

6 Comments

using this it continuous runs it does not stops
Hmmm you're looping through states and then Main.Map and then possibly Main.Map again - kind of problematic but not related to the core question... Still shouldn't go forever although I don't know what the data looks like
I tried another approach and update the question too but it throws error
@Akhi21 You're new way is missing 'retrun on line 6 return subMain ? .. You can only leave out the return if all you're code is being returned...
So do i need to wrap up the whole content inside of return?
|

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.