0

I've created a simple test program in React which displays a piece of text.

import React from 'react';
import componentClass from './components/componentClass';

export default function App () {

    return (
      <h1>example</h1>
    )
  }

What I'm trying to do now is take this example text and make it its own separate component. I've created a class component named componentClass where the example text is displayed.

import React, { Component } from 'react';

export default class componentClass extends Component {

  render() {
    return (
      <h1>example</h1>
    )
  }
}

I'm then rendering this component in App.js.

import React from 'react';
import componentClass from './components/componentClass';

export default function App () {

  return (
    <componentClass />
  )
}

The problem is that nothing is showing up so I'm confused as to why the component isn't being displayed. Below is my index.js file.

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
reportWebVitals();

3 Answers 3

3

You should be using PascalCase when naming React components

working fiddle

class ComponentClass extends React.Component {

  render() {
    return (
      <h1>Component class</h1>
    )
  }
}

function App () {
  return (
    <ComponentClass />
  )
}
  
ReactDOM.render(<App />, document.querySelector("#app"))
Sign up to request clarification or add additional context in comments.

Comments

1

In React, ALl components should have the first letter capitalized which means you need to edit each time you mentioned componentClass and turn it into ComponentClass

1 Comment

I see, I'm so used to camel case that I never thought to use pascal case
0

Try wrapping your return in a react fragment like so:

 return (
   <>
    <ComponentClass />
   </>
  )

1 Comment

No, it still doesn't display.

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.