React Hooks

70 阅读1分钟

useState

let memoizedState : any [] = [] ;

let cursor = 0 ;

export function useState<T>(initialValue: () => T | T) {

    memoizedState[cursor] = memoizedState[cursor] || initialValue ;

    const currentCursor = cursor ;

    function setState(value : T ) {

        memoizedState[currentCursor] = value ;

        cursor = 0 ;
		
        render()
    }

    return [memoizedState[cursor ++],setState]
}