docs(v2): do not recommend using useBaseUrl() hook in most cases (#4126)

* doc: suggest not using useBaseUrl most of the time

* doc: suggest not using useBaseUrl most of the time
This commit is contained in:
Sébastien Lorber 2021-01-29 19:10:38 +01:00 committed by GitHub
parent 9c4bf4e138
commit 140bfbfd90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 23 deletions

View file

@ -195,9 +195,10 @@ export async function loadPlugins({
// TODO remove this deprecated lifecycle soon // TODO remove this deprecated lifecycle soon
// deprecated since alpha-60 // deprecated since alpha-60
// TODO, 1 user reported usage of this lifecycle! https://github.com/facebook/docusaurus/issues/3918
console.error( console.error(
chalk.red( chalk.red(
'plugin routesLoaded lifecycle is deprecated. If you think we should keep this lifecycle, please open a Github issue with your usecase', 'plugin routesLoaded lifecycle is deprecated. If you think we should keep this lifecycle, please report here: https://github.com/facebook/docusaurus/issues/3918',
), ),
); );

View file

@ -222,7 +222,20 @@ const MyComponent = () => {
### `useBaseUrl` ### `useBaseUrl`
React hook to automatically prepend `baseUrl` to a string automatically. This is particularly useful if you don't want to hardcode your config's `baseUrl`. We highly recommend you to use this. React hook to prepend your site `baseUrl` to a string.
:::caution
**Don't use it for regular links!**
The `/baseUrl/` prefix is automatically added to all **absolute paths** by default:
- Markdown: `[link](/my/path)` will link to `/baseUrl/my/path`
- React: `<Link to="/my/path/">link</Link>` will link to `/baseUrl/my/path`
:::
#### Options
```ts ```ts
type BaseUrlOptions = { type BaseUrlOptions = {
@ -231,41 +244,49 @@ type BaseUrlOptions = {
}; };
``` ```
Example usage: #### Example usage:
```jsx {3,11} ```jsx
import React from 'react'; import React from 'react';
import Link from '@docusaurus/Link';
import useBaseUrl from '@docusaurus/useBaseUrl'; import useBaseUrl from '@docusaurus/useBaseUrl';
const Help = () => { const SomeImage = () => {
return ( // highlight-start
<div className="col"> const imgSrc = useBaseUrl('/img/myImage.png');
<h2>Browse the docs</h2> // highlight-end
<p> return <img src={imgSrc} />;
Learn more about Docusaurus using the{' '}
<Link to={useBaseUrl('docs/introduction')}>official documentation</Link>
</p>
</div>
);
}; };
``` ```
:::tip
In most cases, you don't need `useBaseUrl`.
Prefer a `require()` call for [assets](./guides/markdown-features/markdown-features-assets.mdx):
```jsx
<img src={require('@site/static/img/myImage.png').default} />
```
:::
### `useBaseUrlUtils` ### `useBaseUrlUtils`
Sometimes `useBaseUrl` is not good enough. This hook return additional utils related to your site's base url. Sometimes `useBaseUrl` is not good enough. This hook return additional utils related to your site's base url.
- `withBaseUrl`: useful if you need to add base urls to multiple urls at once - `withBaseUrl`: useful if you need to add base urls to multiple urls at once.
```jsx {2,5-7} ```jsx
import React from 'react'; import React from 'react';
import {useBaseUrlUtils} from '@docusaurus/useBaseUrl'; import {useBaseUrlUtils} from '@docusaurus/useBaseUrl';
const Component = () => { const Component = () => {
const urls = ['/a', '/b']; const urls = ['/a', '/b'];
// highlight-start
const {withBaseUrl} = useBaseUrlUtils(); const {withBaseUrl} = useBaseUrlUtils();
const urlsWithBaseUrl = urls.map(withBaseUrl); const urlsWithBaseUrl = urls.map(withBaseUrl);
return <div className="col">{/* ... */}</div>; // highlight-end
return <div>{/* ... */}</div>;
}; };
``` ```

View file

@ -515,15 +515,12 @@ Here's a typical Docusaurus v2 page:
import React from 'react'; import React from 'react';
import Link from '@docusaurus/Link'; import Link from '@docusaurus/Link';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useBaseUrl from '@docusaurus/useBaseUrl';
import Layout from '@theme/Layout'; import Layout from '@theme/Layout';
const MyPage = () => { const MyPage = () => {
const {siteConfig} = useDocusaurusContext(); const {siteConfig} = useDocusaurusContext();
return ( return (
<Layout <Layout title={siteConfig.title} description={siteConfig.tagline}>
title={siteConfig.title}
description={siteConfig.tagline}>
<div className="hero text--center"> <div className="hero text--center">
<div className="container "> <div className="container ">
<div className="padding-vert--md"> <div className="padding-vert--md">
@ -532,7 +529,7 @@ const MyPage = () => {
</div> </div>
<div> <div>
<Link <Link
to={useBaseUrl('/docs/get-started')} to="/docs/get-started"
className="button button--lg button--outline button--primary"> className="button button--lg button--outline button--primary">
Get started Get started
</Link> </Link>