import * as React from 'react';
const useTitleNaive = title => {
React.useEffect(() => {
document.title = title;
}, [title]);
};
export default () => {
useTitleNaive('hello!');
return <>title should be "hello!"</>;
};
import * as React from 'react';
const useTitleNaive = title => {
React.useEffect(() => {
document.title = title;
}, [title]);
};
const Component = ({ title }) => {
useTitleNaive(title);
return <div>component sets the title to {title}</div>;
};
export default () => {
useTitleNaive('page');
return (
<>
page sets title to "page"
<Component title="component" />
</>
);
};
import * as React from 'react';
const Title = () => {
const useTitleNaive = title => {
React.useEffect(() => {
document.title = title;
}, [title]);
};
}
const Component = ({ title }) => {
return <div>
<Title>{title}</Title>
component sets the title to {title}</div>;
};
export default () => {
return (
<>
<Title>Page</Title>
page sets title to "page"
<Component title="component" />
</>
);
};
import { useState, useEffect } from 'react'
import { space, types } from '@space/core'
import { withRouter } from 'react-router-dom'
const DEFAULT_TITLE = 'Space'
function Title ({ location }) {
const [title, setTitle] = useState(DEFAULT_TITLE)
const { pathname } = location
useEffect(() => {
const menuItems = space.get(types.MENU_ITEMS)
const newTitle = findMenuTitle(menuItems, pathname) || DEFAULT_TITLE
document.title = (newTitle === DEFAULT_TITLE) ? newTitle : `SPACE | ${newTitle}`
setTitle(newTitle)
}, [pathname])
return title
}
function findMenuTitle (menuItems, pathname) {
for (const { items } of menuItems) {
for (const { headerTitle, to } of items) {
if (pathname.startsWith(to)) {
return headerTitle
}
}
}
return null
}
export default withRouter(Title)
import * as React from 'react';
import { useLayoutContext } from '../../components/Layout/context';
type TitleProps = { children: string };
const Title = ({ children: title }: TitleProps) => {
const { siteName } = useLayoutContext();
React.useEffect(() => {
document.title = title || siteName;
}, [title]);
return null;
};
export default Title;
const MyPage = () => {
useTitle('Kaya Toast');
return <div />
}
const MyPage = () => {
return <div>
<Title>Kaya Toast</Title>
</div>;
}
we're effectively lying, it's not rendering any tags there and it's calling a side effect hook under the hood
it feels it should stay there even after the component unmounts