Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dispatch to data layer not referenced in window #71

Open
debuggingfuture opened this issue May 18, 2018 · 2 comments
Open

dispatch to data layer not referenced in window #71

debuggingfuture opened this issue May 18, 2018 · 2 comments
Labels

Comments

@debuggingfuture
Copy link

debuggingfuture commented May 18, 2018

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?

@tizmagik
Copy link
Collaborator

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.

@debuggingfuture
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants