react-tracking icon indicating copy to clipboard operation
react-tracking copied to clipboard

dispatch to data layer not referenced in window

Open debuggingfuture opened this issue 7 years ago • 2 comments

Currently process() & dispatch() only take argument data. Does that imply the data layer must be referenced in window? e.g. window. dataLayer

My case is I'm using https://github.com/mobxjs/mobx-react where I would like to put the data layer inside props.rootStore. I don't see a way to have it available inside dispatch(), even the react component is a child of the StoreProvider. I tried to hack around with process() but seems the merge will make observers converted to plain object.

Is dispatch(data,props) feasible?

debuggingfuture avatar May 18 '18 17:05 debuggingfuture

Hey @vincentlaucy good question. Are you saying you want to be able to push data to rootStore (instead of some global window.dataLayer[])? If so, I think you should be able to push to the rootStore with the current API. Would something like this work?

@inject("rootStore")
@track(ownProps => ({
  rootStore: ownProps.rootStore // this came from mobx @inject
}), { 
  // define `dispatch()` as pushing to rootStore that was made available
  // from the first argument into @track()
  dispatch: data => {
    const { rootStore, ...others } = data; // pluck off all data except "rootStore"
    data.rootStore.push(others); // "push" to the rootStore
  }
})
class App extends Component { ... }

Then, any component that is a descendant of <App /> will utilize this dispatch function.

Let me know if I misunderstood your question, happy to talk through it some more.

tizmagik avatar May 21 '18 14:05 tizmagik

Thanks for quick response! I took a quick try but got an error abt max. call stack. Seems some quirks about mobx not playing well with merge() , will investigate a bit more when I have time

debuggingfuture avatar May 21 '18 16:05 debuggingfuture