score:25

Accepted answer

as you said, react is just the v in the mvc. so, where do you put your business logic?

for small, view-specific business logic, it's ok to put that on the component, as you did. if the business logic is in the component, it's going to be on an event-handler, in the render or any other component method.

if you have non-view-specific business logic, and it runs on the client (or both on the client and server), it's always a good practice to isolate that on a separate javascript module. react does not play nice with amd, so you are better off not using requirejs for modularization. you should probably use browserify or webpack. in this case, all you would have to do is add this to the top or your component file: var mybusinesslogic = require('./mybusinesslogic'). now you can delegate processing to this module. this is the prefered way, because javascript modules are easily testable using jest, jasmine, karma or mocha.

there's also a third scenario in which you delegate business logic to the server. you can directly make ajax calls to the api on the server, to do this processing, or you can go with the more sophisticated way and use flux. there's a plethora of flux implementations out there, like alt, redux and fluxxor. i prefer to have my own implementation of flux using the default dispatcher. within the actioncreators i call a method on the clientapi (a js module), which does an ajax call using axios to the server. this call is handled by an express route which finally delegates the business logic to the serverapi.

edit: i just moved to redux :)

score:1

you can push logic inside every class component and the app component. logic in class component for this state and logic in app component for all application

score:3

i know this answer is late but wanted to add my thoughts. usually you want to keep presentation components dumb. meaning your ui components have no logic other then something like a type which may style the component differently. it’s a good idea to pull your logic out into react hooks then using the react hooks in your controlling component. that way all the logic is in your hooks and not in your presentational components making them a lot more independent and reusable.


Related Query

More Query from same tag