Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React components rendering with variable names

I have a unknown list of react components to render.

// this is a react component
var DefaultModule = require('./DefaultModule.js');

<DefaultModule/> <--- this will output something because the name is exactly the class name inside DefaultModule.js

but if I do

// this is a react component
var sssss = require('./DefaultModule.js');

<sssss/> <--- this will not work

so is there a way I can take a list of component names and render them accordingly? Thanks.

p.s: the react component looks like this

import React, {PropTypes, Component} from 'react';

class TestComponent extends Component {

  render() {
    return (
      <div>
     1123123123123123 hahaha small test
      </div>
    );
  }
}

export default TestComponent;
like image 615
Shih-Min Lee Avatar asked Oct 15 '25 09:10

Shih-Min Lee


1 Answers

I just figured out the answer from a discussion thread...

Yes I can do that but the react component must start in capital letter. so Sssss will work sssss will now.

https://github.com/facebook/react/issues/3365

The key is to use a capitalized variable name (Component) otherwise React will treat it as a built-in DOM element.

like image 65
Shih-Min Lee Avatar answered Oct 17 '25 02:10

Shih-Min Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!