-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
尝试增加单词发音功能 #50
Merged
Merged
尝试增加单词发音功能 #50
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
39fa640
feat: 发音开启和关闭的状态切换
whexy 396fe16
bug fix: 点击按钮切换发音状态
whexy aefd303
bug fix: 连续重复发音及切换错误发音
whexy af36a80
feat(us uk pronunciation): 增加英美发音切换
sdu-gyf 677750c
fix(useEffect): 消除控制台错误提示
whexy 19d251e
fix(default pronunciation): switch to us fixed #34
sdu-gyf 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
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,17 +1,21 @@ | ||
import { useAppSettings } from 'components/AppSettings' | ||
import { noop } from 'lodash' | ||
import { useEffect, useState } from 'react' | ||
|
||
declare type PronounceFunction = () => void | ||
|
||
const pronunciationApi = 'http://dict.youdao.com/dictvoice?audio=' | ||
|
||
export default function usepronunciationSound(word: string): [PronounceFunction] { | ||
// SAFETY: We are trying to build an hook here. | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
export default function usePronunciationSound(word: string): PronounceFunction { | ||
const [audio, setAudio] = useState<HTMLAudioElement | null>(null) | ||
const { pronunciation } = useAppSettings() | ||
const pronounceFunction = () => { | ||
var audio = new Audio(pronunciationApi + word) | ||
audio.play() | ||
audio?.pause() | ||
setAudio(new Audio(pronunciationApi + word)) | ||
} | ||
return pronunciation ? [pronounceFunction] : [noop] | ||
|
||
useEffect(() => { | ||
audio?.play() | ||
}, [audio]) | ||
return pronunciation ? pronounceFunction : noop | ||
} |
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 |
---|---|---|
|
@@ -162,6 +162,10 @@ const App: React.FC = () => { | |
(dictName: string) => { | ||
setOrder(0) | ||
setIsLoading(true) | ||
// Need to stop the game, in order to prevent pronounce wrong word. | ||
// Besides, it makes sense because users are about to stop when they change dict/chapter. | ||
// Otherwise, introduce a new parameter to allow pronunciation begin. | ||
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. 这种方案可能确实很是合理的。 |
||
setIsStart(false) | ||
wordListDispatch('setDictName', dictName, () => { | ||
setIsLoading(false) | ||
}) | ||
|
@@ -172,6 +176,7 @@ const App: React.FC = () => { | |
const changeChapter = useCallback( | ||
(chapter: number) => { | ||
setOrder(0) | ||
setIsStart(false) // Same story as above. | ||
wordListDispatch('setChapter', chapter) | ||
}, | ||
[wordListDispatch], | ||
|
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.
这里不应该使用 useLayoutEffect,useLayoutEffect是与dom更新同步,而useEffect是在dom更新后进行。盲目的使用useLayoutEffect可能会引发其他问题。可以参考链接:https://zh-hans.reactjs.org/docs/hooks-reference.html#uselayouteffect
以及,对 useEffect类函数的依赖可能理解不对,这里 hooks 内部没有使用 hasWrong,可以在依赖项中移除。