-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: update to latest react version #39
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
37506db
94e56e2
cd6833c
8799a3f
0708ed2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 interface ReactGitHubButtonProps extends GitHubButtonProps & React.AnchorHTMLAttributes<HTMLAnchorElement> {} | ||
|
|
||
| declare const GitHubButton: React.FC<ReactGitHubButtonProps>; | ||
|
|
||
| export default GitHubButton; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,51 @@ | ||
| 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) { | ||
| render(tempSpan.appendChild(buttonRef.current), (el) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initial state: <span> <!-- containerRef-->
<a> <!-- buttonRef -->
</a>
</span>After adding a <span> <!-- containerRef-->
<a></a> <!-- buttonRef -->
<span></span> <!-- tempSpan -->
</span>With <span> <!-- containerRef-->
<span> <!-- tempSpan -->
<a></a> <!-- buttonRef -->
</span>
</span>Therefore, when restoring the state on error or inside containerRef.current.replaceChild(buttonRef.current, containerRef.current.lastChild);So that it goes back to initial state.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason we have this |
||
| containerRef.current?.contains(tempSpan) && | ||
NdekoCode marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| containerRef.current.replaceChild(el, tempSpan); | ||
| }); | ||
| } | ||
| }) | ||
| .catch((error) => { | ||
| console.error("Error loading github-buttons:", error); | ||
| containerRef.current?.contains(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; | ||
Uh oh!
There was an error while loading. Please reload this page.