1

I am new in react world. So this is may be a silly question I am trying to do something similar to this https://reactjs.org/docs/conditional-rendering.html#inline-if-else-with-conditional-operator but instead of display the strings I want to display properties value

Something like this:

render() {
 const isLoggedIn = this.state.isLoggedIn;
 return (
 <div>
 The user is <b>{isLoggedIn ? {prop1} : {prop2}}</b> logged in.
 </div>
 );
}

If I do that I get an error. But I can see the value of {prop1} and {prop2} if I put them outside of the if clause.

What other ways I could do something like this? Have two returns would be ugly in my optinion because I would have some duplicated code when only what I want is to display some diffrerent string

EDIT: The error message is this

react-dom.development.js:14793 Uncaught Error: Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead.

Radu Diță
14.3k2 gold badges33 silver badges37 bronze badges
asked Apr 24, 2020 at 9:57
4
  • what is the error you are getting? Commented Apr 24, 2020 at 10:01
  • Sorry forgot to add it. The error is react-dom.development.js:14793 Uncaught Error: Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead. Commented Apr 24, 2020 at 10:02
  • try logging the prop1 to console and check Commented Apr 24, 2020 at 10:04
  • use this.props.prop1 and this.props.prop2? Commented Apr 24, 2020 at 10:06

3 Answers 3

3

Assuming that prop1 and prop2 are strings (or numbers) you could do:

render() {
 const isLoggedIn = this.state.isLoggedIn;
 return (
 <div>
 The user is <b>{isLoggedIn ? prop1 : prop2}</b> logged in.
 </div>
 );
}

if they are them-self functions returning React elements :

render() {
 const isLoggedIn = this.state.isLoggedIn;
 return (
 <div>
 The user is <b>{isLoggedIn ? prop1() : prop2()}</b> logged in.
 </div>
 );
}

What you did is to render a javascript object do the dom as is

answered Apr 24, 2020 at 10:06
Sign up to request clarification or add additional context in comments.

Comments

2

depends on what type prop1 & prop2 are but try:

render() {
 const isLoggedIn = this.state.isLoggedIn;
 return (
 <div>
 The user is <b>{isLoggedIn ? prop1 : prop2}</b> logged in.
 </div>
 );
 }
answered Apr 24, 2020 at 10:04

Comments

2

When you use {} in your jsx code, you say react that it's a javascript code, and when you use {} inside {} you try to make an object. And as @Jerome replied you just need to remove the curly brackets. {isLoggedIn ? prop1 : prop2}. Also you can use jsx code inside {}, for example: {isLoading ? <Loading/> : <MainBlock/>}.

answered Apr 24, 2020 at 10:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.