Skip to content

Reading environment variable from .env in React Custom MC app #2427

Answered by emmenko
bengalley-e2x asked this question in Q&A
Discussion options

You must be logged in to vote

Hi, sorry for the late reply.

We don't support using environment variables like in CRA.

However, you can define this kind of information in the Custom Application config file, using the additionalEnv object.

https://docs.commercetools.com/custom-applications/development/application-config#additionalenv

{
  "additionalEnv": {
    "publicKeyTest": "something"
  }
}

These "injected" values can be accessed in the client code using the useApplicationContext hook.

import { useApplicationContext } from '@commercetools-frontend/application-shell-connectors';

const MyComponent = () => {
  const publicKeyTest = useApplicationContext(
    context => context.environment.publicKeyTest
  );
  // ...
};

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kaluginserg
Comment options

@tdeekens
Comment options

Answer selected by emmenko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants