We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
preact 是一个很棒的库,通过其hook的实现代码,可以让你对hook有一种豁达开朗的感觉,这里收集了下代码,方便及时查阅。
function getHookState(index, type) { if (options._hook) { options._hook(currentComponent, index, currentHook || type); } currentHook = 0; const hooks = currentComponent.__hooks || (currentComponent.__hooks = { _list: [], _pendingEffects: [] }); if (index >= hooks._list.length) { hooks._list.push({}); } return hooks._list[index]; }
function useReducer(reducer, initialState, init) { const hookState = getHookState(currentIndex++, 2); hookState._reducer = reducer; if (!hookState._component) { hookState._component = currentComponent; hookState._value = [ !init ? invokeOrReturn(undefined, initialState) : init(initialState), action => { const nextValue = hookState._reducer(hookState._value[0], action); if (hookState._value[0] !== nextValue) { hookState._value = [nextValue, hookState._value[1]]; hookState._component.setState({}); } } ]; } return hookState._value; }
function useState(initialState) { currentHook = 1; return useReducer(invokeOrReturn, initialState); }
function useEffect(callback, args) { const state = getHookState(currentIndex++, 3); if (!options._skipEffects && argsChanged(state._args, args)) { state._value = callback; state._args = args; currentComponent.__hooks._pendingEffects.push(state); } }
function useMemo(factory, args) { const state = getHookState(currentIndex++, 7); if (argsChanged(state._args, args)) { state._args = args; state._factory = factory; return (state._value = factory()); } return state._value; }
function useRef(initialValue) { currentHook = 5; return useMemo(() => ({ current: initialValue }), []); }
function useCallback(callback, args) { currentHook = 8; return useMemo(() => callback, args); }
The text was updated successfully, but these errors were encountered:
useEffect相关
Sorry, something went wrong.
No branches or pull requests
preact中hook的实现代码片段
preact 是一个很棒的库,通过其hook的实现代码,可以让你对hook有一种豁达开朗的感觉,这里收集了下代码,方便及时查阅。
getHookState
useReducer
useState
useEffect
useMemo
useRef
useCallback
The text was updated successfully, but these errors were encountered: