Examples
AppProvider works by default without any additional options passed to it.
App provider is a required component that enables sharing global settings throughout the hierarchy of your application.
AppProvider works by default without any additional options passed to it.
The app provider component is required to use Polaris. Without it, the components in your application will not function correctly. You must wrap the root (the top) of your application in the app provider component. We’ve created several examples to show how that’s done.
Translations are provided in the locales folder. When using Polaris, you are able to import translations from all languages supported by the core Shopify product and consume them through the i18n
prop.
If a project has only one locale, then you can pass the JSON content from the locale file into AppProvider
.
import AppProvider from '@shopify/polaris';
// en.json is English. Replace with fr.json for French, etc
import translations from '@shopify/polaris/locales/en.json';
function App() {
return <AppProvider i18n={translations}>{/* App content */}</AppProvider>;
}
If a project supports multiple locales, then load them dynamically using @shopify/react-i18n
. This ensures that you load only the translations you need.
import AppProvider from '@shopify/polaris';
// en.json is English. Replace with fr.json for French, etc
import translations from '@shopify/polaris/locales/en.json';
import {useI18n} from '@shopify/react-i18n';
function App() {
const [i18n] = useI18n({
id: 'Polaris',
fallback: translations,
translations(locale) {
return import(
/* webpackChunkName: "Polaris-i18n", webpackMode: "lazy-once" */ `@shopify/polaris/locales/${locale}.json`
).then((dictionary) => dictionary && dictionary.default);
},
});
// i18n.translations is an array of translation dictionaries, where the first
// dictionary is the desired language, and the second is the fallback.
// AppProvider however expects that the first dictionary is the fallback
// and the second is the desired language. Thus we need to reverse the array
// to ensure the dictionaries are in the order desired by AppProvider
return (
<AppProvider i18n={i18n.translations.reverse()}>
{/* App content */}
</AppProvider>
);
}
When using Polaris, you don’t need to go through the initialization of the Shopify App Bridge as described in the Shopify Help Center. Instead, configure the connection to the Shopify admin through the app provider component, which wraps all components in an embedded app. The app provider component initializes the Shopify App Bridge using the apiKey
and shopOrigin
that you provide. The apiKey
and the shopOrigin
attributes are required. Find the API key for your app in the Apps section of your Shopify Partner Dashboard. Learn how to get and store the shop origin in the Shopify Help Center.
ReactDOM.render(
<AppProvider apiKey="YOUR_API_KEY" shopOrigin="SHOP_ORIGIN" i18n={{}}>
<ResourcePicker
resourceType="Product"
open={this.state.open}
onSelection={({selection}) => {
console.log('Selected products: ', selection);
this.setState({open: false});
}}
onCancel={() => this.setState({open: false})}
/>
</AppProvider>,
);
As of v3.17.0, using apiKey
and shopOrigin
on AppProvider
to initialize the Shopify App Bridge is deprecated. Support for this will be removed in v5.0 as the underlying Shopify App Bridge library will be removed from Polaris React. Learn more about the deprecation rationale. Use Provider
from @shopify/app-bridge-react
instead.
To provide access to your initialized Shopify App Bridge instance, we make it available through React’s context
. The example below demonstrates how to access the appBridge
object from React’s context
, in order to use the Redirect
action to navigate:
import React from 'react';
import {render} from 'react-dom';
import {AppProvider, _SECRET_INTERNAL_APP_BRIDGE_CONTEXT} from '@shopify/polaris';
import {Redirect} from '@shopify/app-bridge/actions';
function MyApp() {
const appBridge = useContext(_SECRET_INTERNAL_APP_BRIDGE_CONTEXT)
redirectToSettings() {
const redirect = Redirect.create(appBridge);
// Go to {appOrigin}/settings
redirect.dispatch(Redirect.Action.APP, '/settings');
}
render() {
return null;
}
}
render(
<AppProvider
apiKey="YOUR_APP_API_KEY"
shopOrigin="YOUR_SHOP_ORIGIN"
i18n={{}}
>
<MyApp />
</AppProvider>,
document.querySelector('#app'),
);
As of v3.17.0, using the Shopify App Bridge instance in context is deprecated. Support for this will be removed in v5.0 as the underlying Shopify App Bridge library will be removed from Polaris React. More information can be found here. Use the Shopify App Bridge directly instead.
You must include Polaris context in your tests when you use Polaris components.
To make this easier for you, we’ve provided:
createPolarisContext()
function to create the Polaris context for youpolarisContextTypes
variable that contains all the necessary context types