ui-router / redux

UI-Router plugin for Redux integration
MIT License
12 stars 6 forks source link

Duplicate state registration on initialization #13

Open caffed opened 4 years ago

caffed commented 4 years ago

Hi there,

I am using this as part of Electron based application. I am not sure if I am the only one, but I've encountered a duplicate state error when configuring this.

Error: State '${state.name}' is already defined
    at StateQueueManager.register (/home/user/projects/project-electron/node_modules/@uirouter/core/lib/state/@uirouter/core/state/stateQueueManager.ts:35:15)
    at StateRegistry.register (/home/user/projects/project-electron/node_modules/@uirouter/core/lib/state/@uirouter/core/state/stateRegistry.ts:128:28)
    at eval (webpack-internal:///./node_modules/@uirouter/redux/lib/react/ConnectedUIRouter.js:30:49)
    at Array.forEach (<anonymous>)
    at ConnectedUIRouter (webpack-internal:///./node_modules/@uirouter/redux/lib/react/ConnectedUIRouter.js:29:12)
    at renderWithHooks (webpack-internal:///./node_modules/react-dom/cjs/react-dom.development.js:14803:18)
    at mountIndeterminateComponent (webpack-internal:///./node_modules/react-dom/cjs/react-dom.development.js:17482:13)
    at beginWork (webpack-internal:///./node_modules/react-dom/cjs/react-dom.development.js:18596:16)
    at HTMLUnknownElement.callCallback (webpack-internal:///./node_modules/react-dom/cjs/react-dom.development.js:188:14)
    at Object.invokeGuardedCallbackDev (webpack-internal:///./node_modules/react-dom/cjs/react-dom.development.js:237:16)

Upon further inspection it looks like ConnectedUIRouter registers states without checking for their existence first.

Changing this: https://github.com/ui-router/redux/blob/master/react/ConnectedUIRouter.tsx#L35-L37

    (states || []).forEach(state =>
      router.current.stateRegistry.register(state)
    );

to this:

    (states || [])
      .filter(state => !Boolean(router.current.stateRegistry.get(state.name)))
      .forEach(state => router.current.stateRegistry.register(state));

works for me.

kadamgreene commented 4 years ago

I'm seeing the same issue.