Is there any difference in using React component as a (HTML tag)/ (JS function call)

Antajo Paulson

I have a component, something like,

const ComponentA = ({ heading })=> {
 return (<h1>{ heading }</h>);
};

Is there any difference in rendering this component using below two options,

Option 1

const ComponentB = ()=> {
 return ComponentA({ heading: 'Heading test' });
}; 

Option 2

const ComponentB = ()=> {
 return <ComponentA heading='Heading test' />;
}; 
Brian Thompson

Yes. There is a very important difference. In option 1, ComponentA is not actually a component as far as React is concerned. In option 2 it is.

The best way to illustrate the difference is to put state or another hook inside of ComponentA. Option 1 will not work as expected. (Note: if it does happen to work as expected, you still shouldn't trust it. This is where bugs can sneak in because they don't cause issues until later).

Here is an example where using hooks appears to work, but breaks after you get the counter past 5. This is because React is treating the hooks inside ComponentA as if they belong to Example. Notice how the JSX version works as expected even after it disappears.

const {useState, useEffect} = React;

const ComponentA = ({ id, heading })=> {
   React.useEffect(() => {
    console.log(id, 'mounted');
   }, []);
 
 return (
    <h1>
      { heading }
    </h1>
  );
};

const Example = () => {
  const [count, setCount] = useState(0);
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Re-render me</button> {count}
      {count < 5 ? ComponentA({ heading: 'Heading test1', id: 1 }) : null}
      {count < 3 ? <ComponentA heading='Heading test2' id={2} /> : null}
    </div>
   );
}

ReactDOM.render(<Example />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="root"></div>

The reason is JSX (< /> syntax) is actually just a syntax for calling React.createElement. If this is not called, the function component does not get it's own lifecycle, etc. You've bypassed the way React tracks components.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How I call HTML tag function from angular component

From Dev

React: Difference between a Stateful Class Component and a Function Component using Hooks?

From Dev

React JS call function within another file in React Component

From Dev

Creating HTML tag in react component

From Dev

Call vue.js function in html to print value in p tag

From Dev

Call a function from another class component in React.js

From Dev

How to call function in a different component in React.js?

From Dev

function not call on the right html tag

From Dev

Call React Component From Function

From Dev

React | Call a function outside a component

From Dev

Call Function out of React Component

From Dev

Is there any difference using 'return' after Discord.js function?

From Dev

Call JS function on script tag

From Dev

How call React's setState function from outside of any component, like a utility function?

From Dev

In React ES6, is there any benefit to using an imported component that contains only one element tag with a class?

From Dev

How to play video in react.js using html video tag?

From Dev

Call function from React Component from function

From Dev

Call Javascript function in .js file with React from HTML file

From Dev

How to re render the component without using any API call in React Native?

From Dev

react function component inside class component, is a valid component ? What is the difference?

From Dev

Why function doesn't work with a button component but does with a button html tag in VUE js?

From Dev

Using a static script tag in a React component

From Dev

Call a js function on an html snippet rendered using ngBindHtml

From Dev

Using a HTML hyperlink to call a JS function on the parent element

From Dev

React Router difference between component with and without function

From Javascript

call javascript function from anchor html tag

From Dev

React doesn't have the same execution order when calling a component as a function rather than using it as a tag?

From Dev

React: using refs in a function component

From Dev

Using useSelector in React Function Component

Related Related

  1. 1

    How I call HTML tag function from angular component

  2. 2

    React: Difference between a Stateful Class Component and a Function Component using Hooks?

  3. 3

    React JS call function within another file in React Component

  4. 4

    Creating HTML tag in react component

  5. 5

    Call vue.js function in html to print value in p tag

  6. 6

    Call a function from another class component in React.js

  7. 7

    How to call function in a different component in React.js?

  8. 8

    function not call on the right html tag

  9. 9

    Call React Component From Function

  10. 10

    React | Call a function outside a component

  11. 11

    Call Function out of React Component

  12. 12

    Is there any difference using 'return' after Discord.js function?

  13. 13

    Call JS function on script tag

  14. 14

    How call React's setState function from outside of any component, like a utility function?

  15. 15

    In React ES6, is there any benefit to using an imported component that contains only one element tag with a class?

  16. 16

    How to play video in react.js using html video tag?

  17. 17

    Call function from React Component from function

  18. 18

    Call Javascript function in .js file with React from HTML file

  19. 19

    How to re render the component without using any API call in React Native?

  20. 20

    react function component inside class component, is a valid component ? What is the difference?

  21. 21

    Why function doesn't work with a button component but does with a button html tag in VUE js?

  22. 22

    Using a static script tag in a React component

  23. 23

    Call a js function on an html snippet rendered using ngBindHtml

  24. 24

    Using a HTML hyperlink to call a JS function on the parent element

  25. 25

    React Router difference between component with and without function

  26. 26

    call javascript function from anchor html tag

  27. 27

    React doesn't have the same execution order when calling a component as a function rather than using it as a tag?

  28. 28

    React: using refs in a function component

  29. 29

    Using useSelector in React Function Component

HotTag

Archive