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

fix: use unique keys for flow nodes #159

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions examples/nextjs-spa/src/pages/recovery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ const Recovery: NextPageWithLayout = () => {
updateRecoveryFlowBody: values,
})
.then(({ data }) => {
// reset the form data completely
setFlow(null)
// Form submission was successful, show the message to the user!
getFlow(data.id)
Comment on lines -87 to -90
Copy link
Member Author

Choose a reason for hiding this comment

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

Reverting all of these changes from 85cc979 because they just masked the real issue.

setFlow(data)
})
.catch(handleError)

Expand Down
4 changes: 1 addition & 3 deletions examples/nextjs-spa/src/pages/verification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ const Verification: NextPageWithLayout = () => {
updateVerificationFlowBody: values,
})
.then(({ data }) => {
// reset the input fields
setFlow(null)
// Form submission was successful, show the message to the user!
getFlow(data.id)
setFlow(data)
})
.catch(handleError)

Expand Down
3 changes: 1 addition & 2 deletions examples/preact-spa/src/recovery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export const Recovery = () => {
})
.then(({ data: flow }) => {
// Form submission was successful, show the message to the user!
setFlow(null)
getFlow(flow.id)
setFlow(flow)
})
.catch(sdkErrorHandler)
}
Expand Down
3 changes: 1 addition & 2 deletions examples/preact-spa/src/verification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export const Verification = () => {
updateVerificationFlowBody: body,
})
.then(({ data: flow }) => {
setFlow(null)
getFlow(flow.id)
setFlow(flow)
})
.catch(sdkErrorHandler)
}
Expand Down
3 changes: 1 addition & 2 deletions examples/react-spa/src/Recovery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ export const Recovery = () => {
.updateRecoveryFlow({ flow: flow.id, updateRecoveryFlowBody: body })
.then(({ data: flow }) => {
// Form submission was successful, show the message to the user!
setFlow(null)
getFlow(flow.id)
setFlow(flow)
})
.catch(sdkErrorHandler)
}
Expand Down
3 changes: 1 addition & 2 deletions examples/react-spa/src/Verification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ export const Verification = (): JSX.Element => {
updateVerificationFlowBody: body,
})
.then(({ data: flow }) => {
setFlow(null)
getFlow(flow.id)
setFlow(flow)
})
.catch(sdkErrorHandler)
}
Expand Down
34 changes: 21 additions & 13 deletions src/react-components/ory/helpers/filter-flow-nodes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,28 @@ export const FilterFlowNodes = ({

const nodes = filterNodesByGroups(filter)
// we don't want to map the csrf token every time, only on the form level
.filter((node) =>
getInputName(node) === "csrf_token" && !includeCSRF ? false : true,
)
.map((node, k) =>
["hidden"].includes(getNodeInputType(node.attributes))
? {
node: <Node node={node} key={k} {...overrides} />,
hidden: true,
.filter((node) => includeCSRF || !(getInputName(node) === "csrf_token"))
.map((node, k) => ({
node: (
<Node
node={node}
key={
// input node
"name" in node.attributes
Copy link
Contributor

Choose a reason for hiding this comment

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

don't we have TypeGuards for this? I believe we already use ory/integrations here, so might as well use the TypeGuards?

https://github.com/ory/integrations/blob/main/src/ui/index.ts#L87

https://github.com/ory/integrations/blob/main/src/ui/index.ts#L54

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call 👍

? node.attributes.name
: // image node
"src" in node.attributes
? node.attributes.src
: // anchor, text & script node
"id" in node.attributes
? node.attributes.id
: k
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the real fix. Previously a re-render after a flow was updated resulted in the same keys, although the input was vastly different. Because the mapping key here was the same, react did not properly re-render the input fields.
This is why index-based keys are always a bit tricky and should only be used when you are absolutely sure that the other props never change across re-renders.

Copy link
Contributor

Choose a reason for hiding this comment

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

makes sense!

}
: {
node: <Node node={node} key={k} {...overrides} />,
hidden: false,
},
)
{...overrides}
/>
),
hidden: getNodeInputType(node.attributes) === "hidden",
}))
return nodes.length > 0 ? (
<>
{nodes.filter((node) => node.hidden).map((node) => node.node)}
Expand Down
Loading