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

RedirectComponent: refactor to function component #558

Merged

Conversation

markus-codewrangler
Copy link
Contributor

No description provided.

Copy link

netlify bot commented Mar 22, 2024

👷 Deploy request for scrivito-example accepted.

Name Link
🔨 Latest commit a4cf776
🔍 Latest deploy log https://app.netlify.com/sites/scrivito-example/deploys/664f31fe770b5d0008102f71

@apepper apepper self-assigned this Apr 8, 2024
Copy link
Contributor

@apepper apepper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FEEDBACK (d26d91c)

Nice start 🚀! Please see my comments.


render() {
const link = this.props.page.get("link");
}, [page]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to first const link = page.get("link"); and add link to the dependency of useEffect.

Something like:

const link = page.get("link");
React.useEffect(() => {
  if (!link) return;

  Scrivito.load(() => Scrivito.urlFor(link)).then((url) => {
      if (link.isExternal()) {
        window.top.location.replace(url);
      } else {
        window.location.replace(url);
      }
  }
}, [link]);

Copy link
Contributor

@apepper apepper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FEEDBACK (b173bc9)

Nice progress. See my comments for further improvements.

const url = link && Scrivito.urlFor(link);
Scrivito.provideComponent("Redirect", ({ page }) => {
const link = page.get("link");
const url = link && Scrivito.urlFor(link);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the url resolving with a load inside of the useEffect. Otherwise its possible that the useEffect is triggered twice - first with the "unresolved" urlFor(link) and then with the resolved/loaded urlFor(link).

Copy link
Contributor

@apepper apepper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (a4cf776)

Nice 🚀!

@apepper apepper merged commit 7e6d2fa into Scrivito:master May 23, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants