Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useHash using empty string in the first render #5140

Closed
iGoodie opened this issue Oct 27, 2023 · 3 comments
Closed

useHash using empty string in the first render #5140

iGoodie opened this issue Oct 27, 2023 · 3 comments
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)

Comments

@iGoodie
Copy link

iGoodie commented Oct 27, 2023

What package has an issue

@mantine/hooks

Describe the bug

useHash renders with "" in the first render, making it impossible to declare initial state values with it.
Here's an overly simplified version of my case:

  const [hash, setHash] = useHash();

  const [currentTab, setCurrentTab] = useState(isValidTab(hash) ? hash : Tabs.DESCRIPTION);

However, since internally its initial value is set as an effect on the second render, I can't use it as a barebone for a state initializer.

useEffect(() => {
setHashValue(window.location.hash);
}, []);

What version of @mantine/* packages do you have in package.json? (Note that all @mantine/* packages must have the same version in order to work correctly)

6.0.21

If possible, please include a link to a codesandbox with the reproduced problem

No response

Do you know how to fix the issue

Yes

Are you willing to participate in fixing this issue and create a pull request with the fix

Yes

Possible fix

My solution suggestion would be to replace the following line:

const [hash, setHashValue] = useState<string>('');

with

  const [hash, setHash] = useState(window.location.hash);
@iGoodie iGoodie changed the title useHash using empty in the first render useHash using empty string in the first render Oct 27, 2023
rtivital added a commit that referenced this issue Nov 7, 2023
@rtivital rtivital added the Fixed patch Completed issues that will be published with next patch (1.0.X) label Nov 7, 2023
@rtivital
Copy link
Member

rtivital commented Nov 7, 2023

Fixed in 7.2.1. See https://mantine.dev/hooks/use-hash/#initial-state-value

@rtivital rtivital closed this as completed Nov 7, 2023
@iGoodie
Copy link
Author

iGoodie commented Nov 20, 2023

Isn't this statement reversed? @rtivital

export function useHash({ getInitialValueInEffect = false }: UseHashOptions = {}) {
const [hash, setHashValue] = useState<string>(
getInitialValueInEffect ? window.location.hash || '' : ''
);

If the option to use useEffect is set, it uses immediately from window. I think it should be the opposite

getInitialValueInEffect ? '' : window.location.hash || '' 

@rtivital
Copy link
Member

Thanks for reporting, the issue will be resolved in the next minor release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)
Projects
None yet
Development

No branches or pull requests

2 participants