-1

I've passed a prop - {pov} to a component- Page I also have two other functional components - VisitorPOV and OwnerPOV if pov === 'visitor' , I want to render VisitorPOV Component else OwnerPOV Component Here is what I tried

    function Page({pov}) 
    {
      return (
        <div>
          { {pov} === "visitor" ? <VisitorPOV/> : <OwnerPOV/>}
          </div>
          );
     }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

But what ever the prop is, it is only rendering false part ie. the component after :

Please help! Thanks

1
  • 3
    You already have an enclosing {} around the ternary so you are already in a JS expression context rather than jsx. So there is no need to enclose the pov variable with an additional set of braces. In fact doing so creates an object with pov as the key name and the variable value as the value. Since objects are only equal via identity, this will always result in going down the "false" branch of your ternary. In short, just remove the curly braces surrounding pov in your ternary, and it should work. Commented Apr 25, 2022 at 22:15

1 Answer 1

2

You ternary expression should be as follows:

{ pov === "visitor" ? <VisitorPOV/> : <OwnerPOV/>}

I don't need to explain @Crice did explain it in his comment.

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.