This is the main documentation page for the frontend Devcontainer plugin's React hooks.
This hook gives you access to a set of properties that describe whether the currently-viewed entity has Dev Containers data.
export type UseDevcontainersResult = Readonly<{
tagName: string;
hasUrl: boolean;
vsCodeUrl: string | undefined;
}>;
declare function useDevcontainers(): UseDevcontainersResult;const YourComponent = () => {
const state = useDevcontainers();
return (
{state.hasUrl ? (
<>
<p>Your entity supports Dev Containers!</p>
<a href={state.vsCodeUrl}>Click here to launch VS Code</a>
</>
) : (
<p>No Dev Containers plugin tag detected</p>
)}
);
};
<DevcontainersProvider config={devcontainersConfig}>
<YourComponent />
</DevcontainersProvider>;- Will throw a render error if called outside a React component
- Will throw a render error if called outside of a
DevcontainersProvider - Will throw a render error if called outside an
EntityLayout(or any other Backstage component that exposesEntitydata via React Context)
- This hook works by detecting whether the tag that you specify for
DevcontainersConfig(explicitly or implicitly) can be found in the current entity.- The frontend plugin assumes that the tag will automatically be added to relevant entities via something like the backend plugin, but as discussed here, there are limitations around this functionality.
- The types of
hasUrlandvsCodeUrlare defined as part of a discriminated union. As long as you can prove to the compiler thathasUrlistrue(via a type guard or conditional rendering),vsCodeUrlis guaranteed to be of typestring