0

I want Reactjs to add a class named "hello" to my div in html.

I know I can simply do this in JavaScript by writing element.classList.add("hello");

But React shows error for the keyword ".add"

how can I do this?

isherwood
  • 58,414
  • 16
  • 114
  • 157
  • 1
    Your question suggests that you're really new to React and haven't gotten the hang of the fundamental difference between using an MVC-esque library like React, Vue, etc. vs. creating and manually updating elements. I suggest going through a few React tutorials to learn more about that difference. – T.J. Crowder Nov 12 '20 at 12:46
  • 1
    Thanks a lot @T.J.Crowder for your advise, Yes I am new to react so I was trying to convert my vanilla JavaScript projects to react when I came across this problem. But thanks again, I really appreciate your answer and I will surely learn more of react as you suggested. – kulraj chavda Nov 13 '20 at 13:07
  • Does this answer your question? [React Js conditionally applying class attributes](https://stackoverflow.com/questions/30533171/react-js-conditionally-applying-class-attributes)... Sometimes I feel like I'm the only one who searches for answers... – Heretic Monkey Nov 13 '20 at 21:44

1 Answers1

0

You do this by including that class when rendering your component. So for instance, if you're doing a functional component and it returns like this:

return (
    <div>
        This is the content of the div
    </div>
);

you'd change it to:

return (
    <div className={someCondition ? "hello" : ""}>
        This is the content of the div
    </div>
);

...where someCondition controls whether the div should have the hello class or not.

Here's an example that adds the class when you click the div, removes it if you click again, etc.:

const {useState, Component} = React;

function FunctionalExample() {
    const [hasClass, setHasClass] = useState(false);
    const onClick = e => setHasClass(v => !v);
    return (
        <div
            className={hasClass ? "hello" : ""}
            onClick={onClick}
        >
            Example in a functional component
        </div>
    );
}

class ClassExample extends Component {
    constructor(props) {
        super(props);
        this.state = {
            hasClass: false
        };
        this.onClick = this.onClick.bind(this);
    }
    onClick() {
        this.setState(({hasClass}) => ({hasClass: !hasClass}));
    }
    render() {
        const {hasClass} = this.state;
        return (
            <div
                className={hasClass ? "hello" : ""}
                onClick={this.onClick}
            >
                Example in a class component
            </div>
        );
    }
}

ReactDOM.render(
    <div>
        <FunctionalExample />
        <ClassExample />
    </div>,
    document.getElementById("root")
);
.hello {
    color: blue;
    font-style: italic;
}
<div id="root"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.0/umd/react-dom.production.min.js"></script>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Or just className={name} so set the class directly? – Jule Wolf Nov 12 '20 at 13:06
  • 1
    @JuleWolf - If `name` is a variable, yes, or `className="hello"` for a literal value. But the OP's question was about *adding* a class, so I assume it's not there initially and then gets added as the result of some condition becoming true. – T.J. Crowder Nov 12 '20 at 13:20