-
Notifications
You must be signed in to change notification settings - Fork 8
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
refactor: update to latest react version #39
Open
NdekoCode
wants to merge
5
commits into
buttons:main
Choose a base branch
from
NdekoCode:refactor/update-reactjs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−48
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37506db
refactor: update to latest react version
NdekoCode 94e56e2
refactor: extend an intersection type
NdekoCode cd6833c
fix: correct the typescript error ‘’,‘ expected.ts(1005)’ by using ‘t…
NdekoCode 8799a3f
fix: Ensure correct rendering by checking lastChild and handling stal…
NdekoCode 0708ed2
refactor: update package.json
NdekoCode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
import React from 'react' | ||
import { GitHubButtonProps } from 'github-buttons' | ||
import React from 'react'; | ||
|
||
interface ReactGitHubButtonProps extends GitHubButtonProps { | ||
export interface GitHubButtonProps { | ||
href: string; | ||
'data-color-scheme'?: 'no-preference' | 'light' | 'dark'; | ||
'data-icon'?: 'octicon-star' | 'octicon-repo-forked' | 'octicon-eye' | 'octicon-issue-opened' | 'octicon-git-pull-request'; | ||
'data-size'?: 'large' | 'small'; | ||
'data-show-count'?: boolean | 'true' | 'false'; | ||
'data-text'?: string; | ||
'aria-label'?: string; | ||
children?: React.ReactNode; | ||
} | ||
|
||
export default class GitHubButton extends React.PureComponent<ReactGitHubButtonProps> {} | ||
export type ReactGitHubButtonProps = GitHubButtonProps & React.AnchorHTMLAttributes<HTMLAnchorElement>; | ||
|
||
declare const GitHubButton: React.FC<ReactGitHubButtonProps>; | ||
|
||
export default GitHubButton; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,65 @@ | ||
import React, { PureComponent } from 'react' | ||
|
||
class GitHubButton extends PureComponent { | ||
constructor (props) { | ||
super(props) | ||
this.$ = React.createRef() | ||
this._ = React.createRef() | ||
} | ||
render () { | ||
return React.createElement('span', { ref: this.$ }, React.createElement('a', { ...this.props, ref: this._ }, this.props.children)) | ||
} | ||
componentDidMount () { | ||
this.paint() | ||
} | ||
getSnapshotBeforeUpdate () { | ||
this.reset() | ||
return null | ||
} | ||
componentDidUpdate () { | ||
this.paint() | ||
} | ||
componentWillUnmount () { | ||
this.reset() | ||
} | ||
paint () { | ||
const _ = this.$.current.appendChild(document.createElement('span')) | ||
import(/* webpackMode: "eager" */ 'github-buttons').then(({ render }) => { | ||
if (this._.current != null) { | ||
render(_.appendChild(this._.current), function (el) { | ||
try { | ||
_.parentNode.replaceChild(el, _) | ||
} catch (_) {} | ||
}) | ||
import React, { useCallback, useEffect, useRef } from "react"; | ||
|
||
const GitHubButton = React.memo(({ children, ...props }) => { | ||
const containerRef = useRef(null); | ||
const buttonRef = useRef(null); | ||
|
||
const paint = useCallback(() => { | ||
if (!containerRef.current) return; | ||
|
||
const tempSpan = document.createElement("span"); | ||
containerRef.current.appendChild(tempSpan); | ||
|
||
import(/* webpackMode: "eager" */ "github-buttons") | ||
.then(({ render }) => { | ||
if ( | ||
buttonRef.current && | ||
containerRef.current && | ||
containerRef.current.lastChild === tempSpan | ||
) { | ||
render(tempSpan.appendChild(buttonRef.current), (el) => { | ||
if ( | ||
containerRef.current && | ||
containerRef.current.lastChild === tempSpan | ||
) { | ||
containerRef.current.replaceChild(el, tempSpan); | ||
} | ||
}); | ||
} | ||
}) | ||
.catch((error) => { | ||
console.error("Error loading github-buttons:", error); | ||
if ( | ||
containerRef.current && | ||
containerRef.current.lastChild === tempSpan | ||
) { | ||
containerRef.current.removeChild(tempSpan); | ||
} | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
paint(); | ||
|
||
return () => { | ||
if (containerRef.current) { | ||
const lastChild = containerRef.current.lastChild; | ||
if (lastChild && lastChild !== buttonRef.current) { | ||
containerRef.current.removeChild(lastChild); | ||
} | ||
} | ||
}) | ||
} | ||
reset () { | ||
this.$.current.replaceChild(this._.current, this.$.current.lastChild) | ||
} | ||
} | ||
|
||
export default GitHubButton | ||
}; | ||
}, [paint]); | ||
|
||
return ( | ||
<span ref={containerRef}> | ||
<a {...props} ref={buttonRef}> | ||
{children} | ||
</a> | ||
</span> | ||
); | ||
}); | ||
|
||
GitHubButton.displayName = "GitHubButton"; | ||
|
||
export default GitHubButton; |
This file contains 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
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.
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.
Initial state:
After adding a
tempSpan
:With
tempSpan.appendChild(buttonRef.current)
:Therefore, when restoring the state on error or inside
useEffect
, it should be:So that it goes back to initial state.
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.
The reason we have this
tempSpan
is because this library runs async rendering that dispatches outside of react lifecycles, therefore we want to make sure when we come back to react lifecycle, react should not see any DOM patch that we have done so that it can patch DOM incrementally. In order to do that we have to restore the DOM to the state that react generates. The primary goal of using an additionaltempSpan
is allow us to easily check if we have patched the DOM outside of react or not, and acting accordingly.