0

enter image description hereBelow code shows syntax error in ReactJs component:

(that.props.actionType == "opinion")
?
{that.state._CmtCnt?<ViewAnswer isFullView={that.props.isFullView?true:false} />:null}
:
{that.state._CmtCnt?<ViewComment isFullView={that.props.isFullView?true:false} />:null}
1
  • 1
    Probably because you have a syntax error. Without more context, this is going to be difficult to help you with. Commented Jan 15, 2018 at 15:47

3 Answers 3

1

Basic Syntax is:

condition? expression1 : expression2

This is because you are using {} with expression1 and expression2, remove that, {} is required when we want to put JS expressions inside JSX. The way you are using, it means you are trying to return an object and error is because key is not valid.

Write it like this:

(that.props.actionType == "opinion") ?
    (that.state._CmtCnt?<ViewAnswer isFullView={that.props.isFullView?true:false} />:null)
:
    (that.state._CmtCnt?<ViewComment isFullView={that.props.isFullView?true:false} />:null)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mayank, By changing of bracket from {} to () it works.
0

You need to use normal parenthesis for grouping. Braces work only within jsx expressions.

that.props.actionType == "opinion"
  ? (that.state._CmtCnt ? <ViewAnswer isFullView={that.props.isFullView} /> : null)
//  ^                                                                             ^
  : (that.state._CmtCnt ? <ViewComment isFullView={that.props.isFullView} /> : null)
//  ^                                                                              ^

Comments

0

You should write easier to understand code rather than complex nested terinary with little changes between them - all you choose is pick up a component to use, so move that up and you end up with easier to read code with less logic inside JSX

const Renderer = that.props.actionType == "opinion" ? ViewAnswer : ViewComment
// ...

{that.state._CmtCnt && <Renderer isFullView={!!that.props.isFullView} />}
// or
{that.state._CmtCnt ? 
  <Renderer isFullView={!!that.props.isFullView} /> :
  null
}

not sure why you are using that either - aliases for context such as self or that are a bit out of date, you tend to want to keep context to your instances and bind correctly

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.