3

I’m trying to create a React table in which each row is either gray or white. I am doing this by passing rows the props ‘gray’ or ‘white’ upon their creation, but in my MyRow class I’m not sure how to take this prop and convert it to a style.

Relevant code in MyTable.js:

    this.props.items.forEach(function(item) {
        if (i % 2 === 0) {
            rows.push(<MyRow item={item} key={item.name} color={'gray'} />);
        } else {
            rows.push(<MyRow item={item} key={item.name} color={'white'} />);
        }
        i++;
    }.bind(this));

MyRow.js render function:

render() {
    const color = this.props.color;

    const rowColor = {styles.color}; //?? I’m not sure how to do this—how to get color to take on either gray or white?

    return (
        <tr className={styles.rowColor}>
            <td className={styles.td}>{this.props.item.name}</td>
            <td className={styles.editButton}> <Button
                    onClick={this.handleEditButtonClicked}
                />
            </td>
        </tr>
    );
}

MyRow.css:

.td {
    font-weight: 500;
    padding: 0 20px;
}

.gray {
    background-color: #d3d3d3;
}

.white {
    background-color: #fff;
}
1
  • If you already have the CSS classes defined, and you are passing the className as a prop, then just do <tr className={rowColor}>. Why are you piping rowColor through another object called styles. Where is styles defined? It's not included in your code. Commented Jul 15, 2016 at 18:21

2 Answers 2

1

I believe you should be able to pass the prop directly to the row as follows, since it's already defined as a string in MyTable.js file:

render() {
    return (
        <tr className={this.props.color}>
            <td className={styles.td}>{this.props.item.name}</td>
            <td className={styles.editButton}> <Button
                    onClick={this.handleEditButtonClicked}
                />
            </td>
        </tr>
    );
}

Also, from the code you have posted, it's unclear where the styles object is located where you're trying to access these properties. If you wanted to access styles.rowColor, you would need a styles object defined:

const styles = {
    rowColor: color
};
Sign up to request clarification or add additional context in comments.

7 Comments

My styles are defined in styles.css, and then I import styles into the js file.
I'm trying this now <tr className={styles.this.props.color}>, but getting the error "cannot read props of undefined"
Remove the styles from the beginning of that className and you shouldn't get any errors. this.props.color. Thanks for the clarification on the styles variables!
Hmm, but then how will I specify to look in the styles file for either gray or white? this.props.color is just pointing to "gray" or "white", which aren't defined in the js file and are rather accessed by styles.gray/styles.white
It seems like the missing piece from your question / code snippets above is that styles js file. From your original question, it seemed like the styles / className were just being associated with the CSS file posted. If not, provide that styles file instead.
|
0

Follow this to change individual row cell color using row.value

{
  Header: () => <div className="text-center font-weight-bold">Status</div>,
  accessor: "selectedstatus",
  className: "font",
  width: 140,
  Cell: (row) => (
    <div
      className="text-center h-6"
      style={{ background: row.value === "Selected" ? "green" : "red" }}
    >
      {row.value}
    </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.