Polaris-react: Best practice for using the new app bridge context

Created on 9 Jul 2019  路  10Comments  路  Source: Shopify/polaris-react

Issue summary

Feel a tad silly with this one as I feel it is actually probably quite simple.

I'm looking to use the Redirect from the app-bridge. (import {Redirect} from '@shopify/app-bridge/actions').

The issue I'm having is around the app part.

app.dispatch(
  Redirect.toRemote({
    url: 'http://example.com',
  }),
)

Above it mentions doing:

const app = createApp({
  apiKey: '12345',
  shopOrigin: shopOrigin,
});

But given we're already using the <Provider> I'd imagine this is accessible in context somewhere like it used to be?

I've tried going down the createApp route but I'm not sure where I should be saving this to have it accessible globally. Given we're using next I can't use window for example.

Apologies if this is the wrong place to ask this but the app-bridge repos are internal.

Edit

I've noticed when looking at the code of the React components from @shopify/app-bridge-react they use the internal context (var context_1 = require("../../context");) so maybe this isn't globally accessible?

App Bridge

Most helpful comment

ping @timlayton as the man with context on uh, app-bridge context

@dan-gamble it looks like The change to add Context to the root export is in master but hasn't been released just yet. For the moment you can do import {AppBridgeContext} from '@shopify/app-bridge-react/context'; which points to the same thing.

All 10 comments

馃憢 Thanks for opening your first issue. A contributor should give feedback soon. If you haven鈥檛 already, please check out the contributing guidelines. You can also join #polaris on the Shopify Partners Slack.

Hi @dan-gamble we have an example in our docs explaining how to use our app bridge instance. Let me know if this helps!

Hey @AndrewMusgrave,

I did come across that but saw it was deprecated. I wondered how we should be using it going forward?

I'm using the https://github.com/Shopify/shopify-app-node as a base and am currently doing:

class MyApp extends App {
  static async getInitialProps(server) {
    const shopOrigin = server.ctx.query.shop;
    return { shopOrigin };
  }
  render() {
    const { Component, pageProps, shopOrigin } = this.props;
    const app = createApp({
      apiKey: 'API key from Shopify Partner Dashboard',
      shopOrigin: shopOrigin,
    });

    return (
      <Container>
        <AppProvider>
          <Provider
            config={{
              apiKey: API_KEY,
              shopOrigin: shopOrigin,
              forceRedirect: true
            }}
          >
            <ApolloProvider client={localClient}>
              <Component {...pageProps} appBridge={app} />
            </ApolloProvider>
          </Provider>
        </AppProvider>
      </Container>
    );
  }
}

But feel like this is a little bit hacky and I'd have to prop drill to have access to the app bridge.

Would appreciate any guidance on this but it could just be me being relatively naive to all of this.

Looks like this is answered here but the issue i鈥檓 having is import { Context } from '@shopify/app-bridge-react'; as 1.6.6 only exports the components and not the context.

ping @timlayton as the man with context on uh, app-bridge context

@dan-gamble it looks like The change to add Context to the root export is in master but hasn't been released just yet. For the moment you can do import {AppBridgeContext} from '@shopify/app-bridge-react/context'; which points to the same thing.

That鈥檚 awesome, thank you @tmlayton!

I did dip in to my node_modules to see if i could work it out but it鈥檚 a bit harder with the released version vs source

Looks like app-bridge-react 1.6.7 is now released, which includes Context in the root import. import { Context } from '@shopify/app-bridge-react'; should work with that.

Hi Team,
I am having similar issue related to not being able to access global config value in my index.js assigned to in _app.js.
Following this tutorial by shopify:
https://shopify.dev/tutorials/build-a-shopify-app-with-node-and-react/build-your-user-interface-with-polaris

I have tried the above suggestion but Context and AppBridgeContext doesn't work. I would also like to know that since the fix is released for app-bridge-react 1.6.7 but it seems to be that the latest version is only 1.25.0. Is there another version of app-bridge?
Npm: https://www.npmjs.com/package/@shopify/app-bridge-react

Would appreciate it if anybody can point me in the right direction and sorry if this is not the right place to ask. Thank you

I would also like to know that since the fix is released for app-bridge-react 1.6.7 but it seems to be that the latest version is only 1.25.0

Look closer at those minor version numbers, 25 is larger than 6 :)

The app-bridge team recently created a repo where you can report issues to them, For any further app-bridge issues you should open an issue in https://github.com/Shopify/shopify-app-bridge

Hi Scott,

Thanks for pointing that out. I guess the sleepless night figuring out this issue for the last couple of days made me slightly delirious.

Thank you for the link. Will see if similar issue has been raised there or I'll raise one.

Was this page helpful?
0 / 5 - 0 ratings