1

I'm building an application with ReactJS and would like to also use regular JavaScript for a few things, however, I'm having trouble figuring out how I would be incorporating regular JavaScript.

Let's say I have the following React-component:

class Example extends React.Component {
    render() {
        return (
            <h1>I want to be hidden onclick</h1>
        );
    }
}

ReactDOM.render(<Example />, document.getElementById("whateverelement");

How would I use JavaScript in this case to hide the element onclick? Is that even possible?

I tried looking it up, however, I could only find very little results that, unfortunately, did not help very much. For example, one individual suggested defining a global function in seperate script-tags in the same file but I'm not sure if that's a good practice.

Is it common to use regular JavaScript and ReactJS in combination? How would I go about implementing it in this example?

3
  • Possible duplicate of Using a regular javascript library inside a React component Commented Dec 14, 2017 at 7:27
  • 1
    You could use javascript, but you should prefer a more React way for doing it, like onClick event, setState storing a variable isVisible and then either use it in className or do conditional rendering Commented Dec 14, 2017 at 7:34
  • @ric while that question is helpful in my case, unfortunately it does not further explain where to define the function etc. (I suppose .activatemywidget() is the function, but where did that come from, where is it defined). Therefore I must object your claims. Commented Dec 14, 2017 at 7:34

2 Answers 2

1
class Example extends React.Component {
    onClickFunction() {}

    render() {
        return (
            <h1 onclick={onClickFunction}>I want to be hidden onclick</h1>
    );
    }
}

ReactDOM.render(<Example />, document.getElementById("whateverelement");

You can write JS in the React element by opening and closing curly brackets

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

4 Comments

And for the OnClickFunction() I could use a regular old javascript function?
on the onClickFunction you should probably set state e.g. collapsed and return null in the render function if collapsed is true
But technically I could use JS? Because I need to do an XMLHttpRequest to get data from an xml-file (in my actual project) but I wasn't sure if I can just use JavaScript or if there's a react-way of doing it
Yes, you can write regular JavaScript there. It's a Javascript class after all it just extends the React.Component class
1

Example of how you can do this with just react is:

class Example extends React.Component {
constructor(props) {
  super(props)
  this.state = { visibleTitle: false }
}
onClick = () => {
  this.setState({ visibleTitle: !this.state.visibleTitle })
}
render() {
    const { visibleTitle } = this.state; 
    return (
    <div>
        {visibleTitle && <h1 onClick={this.onClick}>I want to be hidden onclick</h1>}
    </div>
    );
}
}

ReactDOM.render(<Example />, document.getElementById("whateverelement");

Ofcourse, modern state management libraries can replace the this.state

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.