troch / reinspect

Use redux devtools to inspect useState and useReducer :mag_right:
MIT License
424 stars 24 forks source link

@@init on redux devtools does not have the initial state #15

Closed BlackFenix2 closed 4 years ago

BlackFenix2 commented 5 years ago

on the demo, the initialstate is not stored untill a single setState() or dispatch() function is invoked. if i use time travel debugging to go back to @@init the state tree goes null on the webpage

image

troch commented 5 years ago

Hello, thanks for the report. Would you mind opening a PR?

BlackFenix2 commented 5 years ago

@troch i can fork the repo and investigate.

BlackFenix2 commented 5 years ago

i found the culprit-ish, the useState and useReducers methods from reinspect are registered after the store is created in the StateInspector, hence the null @@INIT.

export const StateInspector: React.FC<StateInspectorProps> = ({
    name,
    initialState = {},
    children
}) => {

const store: EnhancedStore = createStore(
            storeReducer,
            initialState,
            window.__REDUX_DEVTOOLS_EXTENSION__({
                name: name || "React state",
                actionsBlacklist: ["/_init", "/_teardown"]
            })
        )

if i set the initialState on the StateInspector HOC that state is stored on the @@INIT Stange

const initialState = {
    whereIsTheState: "Over here!"
}

function App() {
    return (
        <StateInspector name="Example" initialState={initialState}>
            <Counters />
        </StateInspector>
    )
}

image

we would need to figure out a way to get the initialState from the useState and useReducer hooks into the HOC. I can work around this issue at work as-is but id like to see if we can do sometihng.

BlackFenix2 commented 5 years ago

i opened a PR to add initialState to Redux Devtools without requiring a manual dispatch. we still have the empty @@INIT to worry about, unless we have discipline to not time-travel too far in the past (we don't). i can still investigate.

https://github.com/troch/reinspect/pull/16