0

I've created a ReactJS component in a .jsx file. Having read and followed the steps as exactly instructed by ReactJS.org here Adding React and found no programmatical errors or syntactical errors in my code, the ReactJS component still doesn't appear in my webpage.

If no syntactical errors or programmatical ones in both my .jsx file and my .html file too and if I followed the 3 steps correctly then does that mean that there were other steps I should have done but I don't know them?

If so, what are these steps?

My own guess is that there is a couple of edits I should do in index.js and in app.js files, shouldn't I?

Note: I have no idea about how to add ReactJS component except only the 3 steps ReactJS.org has written in the link above. What should I do?

Here's my .jsx file (it's named reactcounter.jsx)

import React, {Component} from "react";

const e = React.createElement;

class ReactCounter extends Component
{

state =
{

counter = 0,

}

badgeColours()
{

var colours = "badge badge-pill badge-";

colours += this.state.counter === 0 ? " warning" : "primary";

return colours;

}

addButton = () => {

return this.setState({counter: this.state.counter + 1});

}

removeButton = () => {

return this.state.counter >= 1 ? this.setState({counter: this.state.counter - 1}) : null;

}

render e()
{

return (

<span className={this.badgeColours()}>{this.state.counter}</span>

<button onClick={this.addButton} className=" btn btn-success">Add</button>

<button onClick={this.removeButton} className=" btn btn-danger">Remove</button>

)

}

}

export default ReactCounter;

const domContainer = document.querySelector("#ReactCounterExample");
ReactDOM.render(e(ReactCounter), domContainer);

and my .html file

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>ReactJS Component</title>
</head>
<body>

<div id="ReactCounterExample"></div>

<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="reactcounter.jsx"></script>
</body>
</html>
K. M.
  • 107
  • 7
  • 2
    Please provide code... However, I suspect it may be babel related. https://reactjs.org/docs/add-react-to-a-website.html#quickly-try-jsx – Dom Jul 22 '19 at 19:52
  • 1
    @Dom Done, sir. – K. M. Jul 22 '19 at 20:45
  • Is your file extension `.js` or a `.jsx`? In your HTML it's written ``. – jered Jul 22 '19 at 21:32
  • @jered It's .jsx – K. M. Jul 22 '19 at 21:36
  • Use ``. If you don't include the `type` attribute, babel will not transpile it to regular javascript that the browser understands. – Håken Lid Jul 22 '19 at 21:44
  • Possible duplicate of [Is there a simple way to include a JSX file with script tag?](https://stackoverflow.com/questions/41230145/is-there-a-simple-way-to-include-a-jsx-file-with-script-tag) – Håken Lid Jul 22 '19 at 21:46
  • @HåkenLid this is not a duplicate at all. – K. M. Jul 22 '19 at 21:46
  • Yes it is. The tutorial you are following also says that you have to add a type attribute to the script tag. https://reactjs.org/docs/add-react-to-a-website.html#quickly-try-jsx – Håken Lid Jul 22 '19 at 21:49
  • @HåkenLid Okay, but I'm trying to display a ReactJS component in my webpage. It's not just merely about an attribute. – K. M. Jul 22 '19 at 21:51

0 Answers0