SHARE
TWEET

Untitled

a guest Oct 18th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // inside of my actions.js, my actions look like this...
  2. // because I'm creating a function that returns a function, I have access to dispatch
  3. // through the Thunk middleware. I technically have access to state too, as a second argument
  4. // Because I'm using dispatch here, this specific function can be bound to the redux store
  5. // You could maybe think about it like a singleton?
  6.  
  7. export const createWellsTask = ({ serverId }) => async (dispatch) => {
  8.   const payload = {
  9.     properties: {
  10.       operation: 'list_wells',
  11.       witsml_server: serverId,
  12.     },
  13.   };
  14.   dispatch({
  15.     type: TYPES.CREATE_WELLS_TASK,
  16.   });
  17.   try {
  18.     const response = await post('/v2/tasks', { task: payload });
  19.     dispatch({
  20.       type: TYPES.CREATE_WELLS_TASK_SUCCESS,
  21.       payload: response.toJS(),
  22.     });
  23.   } catch (e) {
  24.     dispatch({
  25.       type: TYPES.CREATE_WELLS_TASK_ERROR,
  26.       payload: e,
  27.     });
  28.   }
  29. };
  30. // Where it comes together inside of container.js
  31. const mapDispatchToProps = {
  32.   subscribeAppForAsset,
  33.   unsubscribeAppFromAsset,
  34.   createWellsTask,
  35. };
  36. export default
  37.   connect(
  38.     mapStateToProps,
  39.     mapDispatchToProps
  40.   )(StreamMapper);
  41.  /**************************/
  42.  
  43.  //it could also be done like this with no 'actions file' (using .then just for variance)
  44.  
  45. const mapDispatchToProps = (dispatch) => {
  46.   createWellsTask: (myArgs) => dispatch({type: MYTYPE, payload: myArgs})
  47.   // You could break up your bound functions here and 'assemble them' inside of your component
  48. }
  49.  
  50. export default
  51.   connect(
  52.     mapStateToProps,
  53.     mapDispatchToProps
  54.   )(StreamMapper);
  55.  
  56. /************************/
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top