0

Take a look on "this.state.winners":

import React, { Component } from "react";

const playingPlayers = [
    {uid: 1, name: 'John'},
    {uid: 2, name: 'Emperor'},
    {uid: 3, name: 'King'}
];

class Queue extends Component {
    constructor(props) {
        super(props);
        this.state = {
          winners: this.props.winners // here is winner players
        }
    }
    addPlayer(player){
        return (
            <div>
                <li>{player.name}</li>
            </div>
        );
    }
    render(){
        return (
            <React.Fragment>
                {playingPlayers.map(player => this.addPlayer(player))}
            </React.Fragment>
        );
    }
}

export default Queue;

Example results:

John
Emperor
King

uid 2 and uid 3 is winner on my array (this.props.winners)

Now, for example, I would like to mark or highlight uid 2 and 3 in the list above.

For example, like this result:

John
Emperor (is winner)
King  (is winner)

How can i do ? how can mark this winners without rebuilding list ?

Also you can see my example in here:

https://stackblitz.com/edit/react-ab4nia?file=index.js

4 Answers 4

3

change your addPlayer(player) function to check if the player is winner as below:

addPlayer(player){
      var listItem,isWinner;
      isWinner = this.props.winners.some(data=>data.uid===player.uid);
        if (isWinner) {
          listItem = <li>{player.name}(is winner)</li>;
        } else {
          listItem= <li>{player.name}</li>
        }
        return (
            <div>
                {listItem}
            </div>
        );
    }  

Check it on stackblitz: https://stackblitz.com/edit/react-plyqf5

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

3 Comments

I would use uid instead of name
@kip, it was just a hint for him to get it going, but changed it to uid just in case assuming his uid is unique
Yes, I was looking for exactly this code! that was perfect. thanks sir
1

Here you go:

import React, { Component } from "react";

const playingPlayers = [
    {uid: 1, name: 'John'},
    {uid: 2, name: 'Emperor'},
    {uid: 3, name: 'King'}
];

class Queue extends Component {
    constructor(props) {
        super(props);
        this.state = {
          winners: this.props.winners
        }
    }
    checkWinner(uid){
      var status = '';
      for (let winner of this.state.winners) {
        if (winner.uid === uid) {
          status = '(winner)'; //change whatever message you want here
          break;
          }

      }
      return status;
    }
    addPlayer(player){
        return (
            <div>
                <li>{player.name} {this.checkWinner(player.uid)}</li>
            </div>
        );
    }
    render(){
        return (
            <React.Fragment>
                {playingPlayers.map(player => 
                  this.addPlayer(player)

                  )
                }
            </React.Fragment>
        );
    }
}

export default Queue;

Comments

1

You can basically check uuids to compare who the winners are. Here is the link that you can check running demo, I made the winner in a bold format for you to see, you can change logic to anything you want https://react-7sd9h7.stackblitz.io

import React, { Component } from "react";

const playingPlayers = [
  { uid: 1, name: "John" },
  { uid: 2, name: "Emperor" },
  { uid: 3, name: "King" }
];

class Queue extends Component {
  constructor(props) {
    super(props);
    this.state = {
      winners_id: this.props.winners.map(winner => winner.uid) // here is winner players
    };
  }
  addPlayer(player) {
    return (
      <div key={player.uid}>
        {this.state.winners_id.includes(player.uid) ? (
          <li style={{ fontWeight: "bold" }}>{player.name}</li>
        ) : (
          <li>{player.name}</li>
        )}
      </div>
    );
  }
  render() {
    return (
      <React.Fragment>
        {playingPlayers.map(player => this.addPlayer(player))}
      </React.Fragment>
    );
  }
}

export default Queue;

1 Comment

No problem. Please upvote if you think the answer could be useful for others @CarolinaRamirez
0

Add the winner check in addPlayer method and do the conditional display of string. (similarly you can do with styles).

function addPlayer(player) {
  const winners = [2, 3];
  return (
    <div>
      <li>{`${player.name}${
        winners.includes(player.uid) ? " (is Winner)" : ""
      }`}</li>
    </div>
  );
}

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.