zackify zackify - 2 months ago 10
React JSX Question

Is it possible to use React without rendering HTML?

I was wondering if it's possible to use React for doing logic and sending data back to a javascript function, without rendering any html. The component I'm thinking of is something that you pass some data to, and it'll send data back to a javascript function outside of react. I know that can be done, and I've done that part myself, but I'm not sure how you would do this without rendering html as it is required. Is this even a practical use case for react?

Answer

he component I'm thinking of is something that you pass some data to, and it'll send data back to a javascript function outside of react.

Why would you want to create a component for that? Isn't some regular javascript function enough? This seems strange to use a React component if you don't want to produce any HTML.

Anyway you can probably create a component in which the render function is something like that:

render: function() { 
   return false; 
}

or

render: function() { 
   return null; 
}

Returning undefined does not work. Also, for statelesss functional components, previous versions of React did not permit to return false or null but now it's possible.


Note that React can also run on the server side so I guess it is possible to use it in such a way that it doesn't involve any DOM modifications (but maybe only the virtual DOM computation).