modify: react useage in README#50
Open
0x-stan wants to merge 1 commit into
Open
Conversation
Owner
|
Has there been a change about useEffect functionality recently that it needs a return now? Or is it about Next.js? |
Author
|
I think is react. Had test with a empty react project, it appears the same problem. I don't know the root cause yet, but it can be seen from the log that useEffect is executed twice, and the npx create-react-app test_change_theme
cd test_change_themenpm i theme-change --save// src/app.tsx
import { useEffect } from "react";
import { themeChange } from "theme-change";
function App() {
useEffect(() => {
console.log("themeChange useEffect"); // this will log twice
themeChange(false);
// 👆 false parameter is required for react project
}, []);
return (
<div className="App">
...
<button data-toggle-theme="dark,light">toggle theme</button>
...
</div>
);
}// package.json
...
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"theme-change": "^2.5.0",
"web-vitals": "^2.1.4"
},
... |
|
That's probably because of the React strict mode. The existing usage example does not work under the strict mode, which is perhaps not fine. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Modify react useage code in README.
In the original example, within a React (especially Next.js) project,
data-toggle-thememay not work as expected. For instance, usingdata-toggle-theme="dark,light"could result in the theme always switching to dark, without the ability to switch to light.