From 19cc54dcea9c776f228565a58e52c805e9090012 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Mon, 19 Jun 2023 13:31:22 -0400 Subject: [PATCH] add support for init func arg --- _lib_/react.tsx | 4 ++-- example/app.tsx | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/_lib_/react.tsx b/_lib_/react.tsx index b8c9f8f..11bb7fe 100644 --- a/_lib_/react.tsx +++ b/_lib_/react.tsx @@ -102,10 +102,10 @@ const ProxyState =(argNew:StateType)=> }; type Storelike = Record -const ProxyReducer =(inReducer:(inState:Storelike, inAction:string)=>Storelike, inState:Storelike)=> +const ProxyReducer =(inReducer:(inState:Storelike, inAction:string)=>Storelike, inState:Storelike, inInit?:(inState:Storelike)=>Storelike)=> { const check = MapIndex(HMR.statesOld, HMR.statesNew.size); - const argOld = check ? check[1].state : inState; + const argOld = check ? check[1].state : (inInit ? inInit(inState) : inState); const intercept =(inInterceptState:Storelike, inInterceptAction:string)=> { diff --git a/example/app.tsx b/example/app.tsx index d0029d4..3d5d8ba 100644 --- a/example/app.tsx +++ b/example/app.tsx @@ -24,10 +24,16 @@ const reducer =(inState:Store, inAction:number)=> return {...inState, age:inState.age+inAction}; } +const builder =(inState:Store):Store=> +{ + inState.age = 100; + return inState; +} + export default ()=> { - const [Store, Dispatch] = React.useReducer(reducer, {name:"seth", age:24} as Store) + const [Store, Dispatch] = React.useReducer(reducer, {name:"seth", age:24} as Store, builder) return

Title!