-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
[email protected] block javascript:void(0);
#16592
Comments
This was part of the 16.9 release. See https://reactjs.org/blog/2019/08/08/react-v16.9.0.html#deprecating-javascript-urls |
@aweary is there a React recommended way to get the previous behavior of |
You could use |
We encountered this issue too. The following didn't work for us as it would refresh the page:
What did work was using the NavLink inside the react-router-dom library. We were already using it and it perfectly mimics the javascript:void(0) behaviour (but without the warning!).
|
@aweary If the ability to use |
@aweary I agree with raxod502 I have a specific problem where I am using an API for authentication. If I change the element from a anchor to a button the API no longer works because the js that is not exposed to me is still looking for an a tag. I will be forced to put inline js for prevent default and change the href to a hash tag which I do not think is a good solution. |
See my reply in #16382 (comment). |
When I posted my original comment, I was under the mistaken impression that you could not work around the problem by setting Could this be prominently documented? Finding out about it deep in the issue tracker is a very bad user experience. |
This change is unnecessarily user hostile. What's the rationale for not whitelisting |
This is how I worked around it (created a new component):
|
<a href></a> To make sure its cross browser compatible use unused ID. <a href="#nolink"></a>
|
The first one would throw a warning as follows: |
Another solution would be to not specify the
In HTML5, using an No errors, no warnings. The only thing to keep in mind is that the |
This warning is coming up for me using next.js's next/link tag, which requires a value for href, even when simply using it for onPress. None of the suggestions so far have been appropriate as a replacement for javascript:void(0). Passing a href value and using it with e.preventDefault() isn't appropriate because there may be other effects you do want to capture (e.g. a dropdown registering the click and closing itself). |
The following method can avoid React errors: <a href={maybeEmpty ?? 'blob:'>...</a> The disadvantage is that clicking it will trigger a console error. |
Warning: A future version of React will block javascript: URLs as a security precaution. Use event handlers instead if you can. If you need to generate unsafe HTML try using dangerouslySetInnerHTML instead. React was passed "javascript:void(0);".
[email protected]
The text was updated successfully, but these errors were encountered: