MattJ MattJ - 22 days ago 8
React JSX Question

ReactJs where to put reusable logic

I am coming from a AngularJs background and trying to learn ReactJs, all the component, reduces, action thing seems quick clear, but one thing for me is missing. What is the alternative for angularJs service/factory, where do I put reusable logic, for now I just put them in separate modules like example:

export default {
loggedIn: () => {
var accessToken = localStorage.getItem('access_token');
if (!accessToken)
return false;
return true;
},

getToken : () => {
let token = localStorage.getItem('access_token');
if (token !== null)
return token;
},
getBearerToken: () => {
let token = localStorage.getItem('access_token');
if (token !== null)
return 'bearer ' + token;
}
}


And then just require it in. If this is not one of the ways, then where should it be placed?

Answer

You are right! Just make a module and require it.
I usually add my http related files in a directory called 'services' or 'api'. You can also create a directory called 'helpers' ant put logic content to avoid repeat yourself.