2

Instagram clone:

I want to check if I follow my follower then display <button>unfollow</button> or if not display <button>follow</button>

var IfollowAndHeFollowMe;

myfollowers.map(follower => {
     return(
       <div>
          <div>{follower.userName}</div>
          { IfollowAndHeFollowMe = 
            myfollowings.filter((following) => following.userName == follower.userName)
          }


          // this doesn't work

         { IfollowAndHeFollowMe.length > 0 ? return( <button>unfollow</button>): return(<button>follow</button>) }


         // and this also doesn't work

         { return IfollowAndHeFollowMe.length > 0 ? <button>unfollow</button> : <button>follow</button>}


       </div>
    )
})

//https://instagram-app-clone.netlify.app/ --- just for phone ---

0

2 Answers 2

4

JSX Code inside {} should be written as statements.

https://reactjs.org/docs/conditional-rendering.html

const followers = ['John', 'Hanna'];

function RenderMap() {
    return (
        <React.Fragment>
            {followers.map(follower => (
                <div>
                    <span>{follower}</span>
                    {followers.length > 0 ? (
                        <button>unfollow</button>
                    ) : (
                        <button>follow</button>
                    )}
                </div>
            ))}
        </React.Fragment>
    );
}
<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>

Sign up to request clarification or add additional context in comments.

25 Comments

without return??
Yes, this is how Javascript Ternary operators works. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
Uncaught Error: Objects are not valid as a React child (found: object with keys {userName, name, profImg, posts}). If you meant to render a collection of children, use an array instead.
{ IfollowAndHeFollowMe = myfollowings.filter(following => following.userName == follower.userName) }
and this is bad , can y help me?
|
0

// now dont have errors but its allways display button with follow text

{Data[2][0].followers.map(follower => {
                idFollowers++;

                return (

                    <div className="follower" id={idFollowers}  >
                        <img src={follower.profilna} className="profilnaFollower" alt="" />

                        <div className="userInfoFollower">
                            <h3>{follower.userName}</h3>
                            <p>{follower.name}</p>
                        </div>

                        {dbFollow = Object.entries(Data[2][0].following).filter(following => following.userName == follower.userName)}
                        {dbFollow.length > 0 ? (<button>unfoldasdaslow</button>) : (<button>fodsadsllow</button>)}
                    </div>
                )
            })}
        </div>

14 Comments

Seems to be looking right, what is the error?
its cant dbFollow be object because following1 is object. must be array or string , it will set following1 with .filter() but i want to dbFollow be following1.userName not object
Objects are not valid as a React child (found: object with keys {userName, name, profilna, posts}). If you meant to render a collection of children, use an array instead.
If you get this error you shared above: Uncaught Error: Objects are not valid as a React child (found: object with keys {userName, name, profImg, posts}). If you meant to render a collection of children, use an array instead. Then the problem is that you are using Array methods over an Object.
the problem is here: {dbFollow = following.filter(following1 => following1.userName == follower.userName
|

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.