question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Neither selector or redux gets updated inside component

See original GitHub issue

Summary

I don’t know if this is a bug or a question. I have an event listener with a selector in the Redux wrapper, this works fine.

ReduxWrapper.js

import React from 'react';
import { Provider } from 'react-redux';
import { createStore as reduxCreateStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk'
import { logger } from 'redux-logger'

import rootReducer from '.';
import { screenResize } from '../actions/uiActions'

const createStore = () => process.env.NODE_ENV === 'development' ? reduxCreateStore(rootReducer, applyMiddleware(thunk, logger)) : reduxCreateStore(rootReducer, applyMiddleware(thunk));

window.addEventListener('resize', () => {
  createStore().dispatch(screenResize())
})


export default ({ element }) => (
  <Provider store={createStore()}>{element}</Provider>
);

The reducer updates properly also

uiReducer.js

const uiReducer = (state = initialState, action) => {
  switch (action.type) {
    case types.SCREEN_RESIZE:
      return { ...state, screenHeight: action.screenHeight, screenWidth: action.screenWidth };

    default:
      return state
  }
}

Then the component is something like

const ComponentName = ({ screenWidth, isMobile }) => {
   return `Screen Width is: ${screenWidth}`;
}

ComponentName.propTypes = {
  screenWidth: PropTypes.number,
  isMobile: PropTypes.bool
}

const mapStateToProps = (state) => {
  return {
    isMobile: isMobile(state),
    screenWidth: state.ui.screenWidth
  }
}

export default connect(mapStateToProps)(ComponentName)

I also tried to convert to class component, use getDerivedStateFromProps, and it still does not update, neither the screenWidth, nor the isMobile.

If dispatch actions from the component, like I am doing somewhere else, it works, this that gets dispatched from outside the component does not work

Relevant information

Environment (if relevant)

System: OS: Windows 7 6.1.7601 CPU: (4) x64 AMD A6-3670 APU with Radeon™ HD Graphics Binaries: Node: 13.7.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.2.1 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 6.13.4 - C:\Program Files\nodejs\npm.CMD Languages: Python: 2.7.17 - /c/Python27/python npmPackages: gatsby: ^2.19.7 => 2.19.21 gatsby-background-image: ^0.10.2 => 0.10.2 gatsby-image: ^2.2.39 => 2.2.41 gatsby-plugin-apollo: ^3.0.1 => 3.0.1 gatsby-plugin-manifest: ^2.2.39 => 2.2.42 gatsby-plugin-offline: ^3.0.32 => 3.0.35 gatsby-plugin-postcss: ^2.1.20 => 2.1.20 gatsby-plugin-react-helmet: ^3.1.21 => 3.1.22 gatsby-plugin-robots-txt: ^1.5.0 => 1.5.0 gatsby-plugin-sharp: ^2.4.7 => 2.4.7 gatsby-plugin-stylus: ^2.1.21 => 2.1.21 gatsby-source-filesystem: ^2.1.46 => 2.1.48 gatsby-transformer-sharp: ^2.3.17 => 2.3.17

File contents (if changed)

gatsby-config.js: N/A package.json: N/A gatsby-node.js: N/A gatsby-browser.js:

export { default as wrapRootElement } from './src/state/ReduxWrapper';

gatsby-ssr.js:

export { default as wrapRootElement } from './src/state/ReduxWrapper';

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
piehcommented, Apr 2, 2020

I assumed gatsby-browser only here, because of window.addEventListener which would fail in ssr (window is not defined), but yeah - redux store should probably be recreated for each page (it somewhat depends on the state shape and actions you dispatch on initial render - you might not to re-init store for each page).

In any case, I do think ResizeWatcher is the way to go here 😃

1reaction
Js-Brechtcommented, Apr 3, 2020

@algebrathefish you indicated on spectrum that the problem is solved. I think we can close this issue now 🙂

If you have any more questions, don’t hesitate to ask!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Which components are re-rendering when a state on React ...
It will rerender the component where you have used the redux store value, Either using useSelector or mapStateToProps you subscribe store.
Read more >
Redux Selector - Medium
Selectors are convenience functions used for looking up and retrieving snippets of data from the Redux store, into your components.
Read more >
Deriving Data with Selectors - Redux
A "selector function" is any function that accepts the Redux store state (or part of the state) as an argument, and returns data...
Read more >
Actions and reducers: updating state - Human Redux
reduce when reducing an array, our reducer function takes a starting state and returns either the unchanged state , or a new state...
Read more >
Getting Data Out of the Redux Store with Selectors
The connect() function in react-redux checks if this result is the same as before and will not update a component if its props...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found