Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
184
rated 0 times [  186] [ 2]  / answers: 1 / hits: 69198  / 10 Years ago, fri, february 28, 2014, 12:00:00

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?


More From » reactjs

 Answers
25

As of React >= 16.2 it is possible to use any of these versions:



render() { 
return false;
}

render() {
return null;
}

render() {
return [];
}

render() {
return <React.Fragment></React.Fragment>;
}

render() {
return <></>;
}


Returning undefined does not work.







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.




Why would you want to create a component for that? Most of the time a regular js function in an existing component can be enough.



One usecase is for exemple to setup a side-effect when component is mounted and tear it down when it unmounts. For exemple if you have a ReactNative mobile app with portrait orientation, you could imagine a <Landscape/> component, that, when mounted, would allow temporarily to display the app in landscape orientation, and when unmounted, orientation would be reset to app default. You can surely manage this orientation change on an existing component, but creating a dedicated component might be more handy and reusable.



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).


[#72233] Thursday, February 27, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
victorr

Total Points: 193
Total Questions: 86
Total Answers: 105

Location: Pitcairn Islands
Member since Thu, Jun 24, 2021
3 Years ago
victorr questions
Fri, Nov 13, 20, 00:00, 4 Years ago
Sat, Jul 25, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;