Skip to content

Commit

Permalink
chore(docs): corrected react usage example (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
LanceMoe authored Oct 19, 2024
1 parent bd81b7c commit 15f2fb8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
41 changes: 23 additions & 18 deletions docs/docs/en/develop/import-framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,37 @@ onBeforeUnmount(() => {
::: code-group

```tsx [React Hooks]
import React, { useEffect, useRef } from 'react'
import { useCallback, useRef } from 'react'
import { useLocation } from 'react-router-dom'
import 'artalk/Artalk.css'
import Artalk from 'artalk'

const ArtalkComment = () => {
const container = useRef<HTMLDivElement>(null)
const location = useLocation()
const { pathname } = useLocation()
const artalk = useRef<Artalk>()

useEffect(() => {
artalk.current = Artalk.init({
el: container.current!,
pageKey: location.pathname,
pageTitle: document.title,
server: 'http://localhost:8080',
site: 'Artalk Blog',
// ...
})

return () => {
artalk.current?.destroy()
}
}, [location.pathname])
const handleContainerInit = useCallback(
(node: HTMLDivElement | null) => {
if (!node) {
return
}
if (artalk.current) {
artalk.current.destroy()
artalk.current = undefined
}
artalk.current = Artalk.init({
el: node,
pageKey: pathname,
pageTitle: document.title,
server: 'http://localhost:8080',
site: 'Artalk Blog',
// ...
})
},
[pathname],
)

return <div ref={container}></div>
return <div ref={handleContainerInit}></div>
}

export default ArtalkComment
Expand Down
41 changes: 23 additions & 18 deletions docs/docs/zh/develop/import-framework.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,37 @@ onBeforeUnmount(() => {
::: code-group

```tsx [React Hooks]
import React, { useEffect, useRef } from 'react'
import { useCallback, useRef } from 'react'
import { useLocation } from 'react-router-dom'
import 'artalk/Artalk.css'
import Artalk from 'artalk'

const ArtalkComment = () => {
const container = useRef<HTMLDivElement>(null)
const location = useLocation()
const { pathname } = useLocation()
const artalk = useRef<Artalk>()

useEffect(() => {
artalk.current = Artalk.init({
el: container.current!,
pageKey: location.pathname,
pageTitle: document.title,
server: 'http://localhost:8080',
site: 'Artalk 的博客',
// ...
})

return () => {
artalk.current?.destroy()
}
}, [container, location.pathname])
const handleContainerInit = useCallback(
(node: HTMLDivElement | null) => {
if (!node) {
return
}
if (artalk.current) {
artalk.current.destroy()
artalk.current = undefined
}
artalk.current = Artalk.init({
el: node,
pageKey: pathname,
pageTitle: document.title,
server: 'http://localhost:8080',
site: 'Artalk 的博客',
// ...
})
},
[pathname],
)

return <div ref={container}></div>
return <div ref={handleContainerInit}></div>
}

export default ArtalkComment
Expand Down

0 comments on commit 15f2fb8

Please sign in to comment.