Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

违背hooks原则 #73

Closed
mankeheaven opened this issue Jul 6, 2021 · 1 comment
Closed

违背hooks原则 #73

mankeheaven opened this issue Jul 6, 2021 · 1 comment

Comments

@mankeheaven
Copy link

hooks是依赖顺序的,以下代码应该会报错,至于为啥违反了react原则又没报错,这个很有水准

export function useObservableInternal<TOutput, TInputs extends Readonly<any[]>>(
  useCustomEffect: typeof useEffect,
  init:
    | (() => Observable<TOutput>)
    | ((inputs$: Observable<[...TInputs]>) => Observable<TOutput>),
  inputs?: [...TInputs]
): Observable<TOutput> {
  if (!inputs) {
    return useRefFn(init as () => Observable<TOutput>).current
  }

  const inputs$Ref = useRefFn(() => new BehaviorSubject(inputs))
  const source$Ref = useRefFn(() => init(inputs$Ref.current))

  const firstEffectRef = useRef(true)
  useCustomEffect(() => {
    if (firstEffectRef.current) {
      firstEffectRef.current = false
      return
    }
    inputs$Ref.current.next(inputs)
  }, inputs)

  return source$Ref.current
}
@crimx
Copy link
Owner

crimx commented Jul 6, 2021

https://observable-hooks.js.org/zh-cn/api/#useobservablestate
见文档说明,这是兼容了两个不一样的使用场景,所以不会冲突。但如果让我再设计一遍可能不会这么骚了嘻嘻😁。

@crimx crimx closed this as completed Jul 6, 2021
Repository owner locked and limited conversation to collaborators Jul 6, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants