-
Notifications
You must be signed in to change notification settings - Fork 76
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
News Site Next: simplify message popup integration #471
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems okay to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good but unfortunately this doesn't work like you expect :) See explanations below
useEffect(() => { | ||
setShowMessage(content[id].message); | ||
}, [id]); | ||
const [showMessage, setShowMessage] = useState(content[id].message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this works only if id
is constant. Indeed the parameter defines only the default value and is taken into account only at the first render.
Depending on how the router works, the behavior might be surprising. And in this case it produces IMO a bug: with this change, the message isn't shown if you don't start the app on the US section.
You can try this STR:
- Open the app: http://127.0.0.1:8080/resources/newssite/news-next/dist/
- Click the "US" section
=> Previously, the message would be displayed. After the change, the message isn't displayed.
Also this STR has changed:
- Open the app on the US section directly: http://127.0.0.1:8081/resources/newssite/news-next/dist/#/us => the message is displayed
- Close the message bar
- Display another section (eg World)
- Display the US section again
=> Previously, the message would be displayed again. After the change, the message isn't displayed again.
With the router (src/pages/index.js
) every route uses the Page
component. So React being clever, the same component is reused and only the properties are updated. As a result, the state isn't changed (remember what I wrote above: what you pass as a parameter to useState
is used only at the first render).
To avoid the update, you can force the Page component to be remounted, by using a key
prop in src/pages/index.js
. A different key will force a different component instance. It might be cleaner actually, and easier to deal with the state inside the Page later, so I'd be in favor of doing that.
Alternatives are to not do the change, or use useLayoutEffect
like you wanted to (according to the branch name).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be good to make showMessage
always a boolean, for example using:
Boolean(content[id].message);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I noticed src/partials/page/page.jsx
has the same pattern for notification
, it could be good to handle it in the same PR.
This pr fixes the issue #441 for news site next.
Previously, we used an useEffect hook, which is async, to determine if a page should display a message to the user or not.
Since we have the data for the page already available, we don't have to use a hook to make that determination.
Previous logic:
new logic:
Scores - main:
Scores - pr: