Thursday, June 28, 2018

reactjs - Having services in React application



I'm coming from the angular world where I could extract logic to a service/factory and consume them in my controllers.




I'm trying to understand how can I achieve the same in a React application.



Let's say that I have a component that validates user's password input (it's strength). It's logic is pretty complex hence I don't want to write it in the component it self.



Where should I write this logic? In a store if I'm using flux? Or is there a better option?


Answer



The first answer doesn't reflect the current Container vs Presenter paradigm.



If you need to do something, like validate a password, you'd likely have a function that does it. You'd be passing that function to your reusable view as a prop.




Containers



So, the correct way to do it is to write a ValidatorContainer, which will have that function as a property, and wrap the form in it, passing the right props in to the child. When it comes to your view, your validator container wraps your view and the view consumes the containers logic.



Validation could be all done in the container's properties, but it you're using a 3rd party validator, or any simple validation service, you can use the service as a property of the container component and use it in the container's methods. I've done this for restful components and it works very well.



Providers



If there's a bit more configuration necessary, you can use a Provider/Consumer model. A provider is a high level component that wraps somewhere close to and underneath the top application object (the one you mount) and supplies a part of itself, or a property configured in the top layer, to the context API. I then set my container elements to consume the context.




The parent/child context relations don't have to be near each other, just the child has to be descended in some way. Redux stores and the React Router function in this way. I've used it to provide a root restful context for my rest containers (if I don't provide my own).



(note: the context API is marked experimental in the docs, but I don't think it is any more, considering what's using it).





//An example of a Provider component, takes a preconfigured restful.js
//object and makes it available anywhere in the application
export default class RestfulProvider extends React.Component {
constructor(props){

super(props);

if(!("restful" in props)){
throw Error("Restful service must be provided");
}
}

getChildContext(){
return {
api: this.props.restful

};
}

render() {
return this.props.children;
}
}

RestfulProvider.childContextTypes = {
api: React.PropTypes.object

};





Middleware



A further way I haven't tried, but seen used, is to use middleware in conjunction with Redux. You define your service object outside the application, or at least, higher than the redux store. During store creation, you inject the service into the middleware and the middleware handles any actions that affect the service.



In this way, I could inject my restful.js object into the middleware and replace my container methods with independent actions. I'd still need a container component to provide the actions to the form view layer, but connect() and mapDispatchToProps have me covered there.




The new v4 react-router-redux uses this method to impact the state of the history, for example.





//Example middleware from react-router-redux
//History is our service here and actions change it.

import { CALL_HISTORY_METHOD } from './actions'


/**
* This middleware captures CALL_HISTORY_METHOD actions to redirect to the
* provided history object. This will prevent these actions from reaching your
* reducer or any middleware that comes after this one.
*/
export default function routerMiddleware(history) {
return () => next => action => {
if (action.type !== CALL_HISTORY_METHOD) {
return next(action)
}


const { payload: { method, args } } = action
history[method](...args)
}
}




No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...