Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not rerender when values changes #197

Closed
Herdismaria opened this issue Jan 29, 2019 · 10 comments
Closed

Does not rerender when values changes #197

Herdismaria opened this issue Jan 29, 2019 · 10 comments

Comments

@Herdismaria
Copy link

Tried using the new version but it's still not working.
GetDerivedStateFromProps returns null even though values object changes.
I think the we need to store forceStatic in the state.

when we store forcesStatic in the state it triggers a rerender if we omit it after first render on the client.
screen shot 2019-01-29 at 13 52 38

The same but without storing forceStatic in the state
screen shot 2019-01-29 at 13 55 46

@Herdismaria
Copy link
Author

We are not using the values object to construct the query so even thought it changes the query does not change so getDerivedStateFromProps returns null?

yocontra added a commit that referenced this issue Jan 29, 2019
@yocontra
Copy link
Owner

@Herdismaria I just made a branch called static-fix, can you try that and see if it works for you?

@Herdismaria
Copy link
Author

const getValues = ({ values }) => {
  if (!values) return null
  const keys = Object.keys(values)
  if (keys.length === 0) return null
  return keys.reduce((result, key) => {
    result[hyphenate(key)] = values[key]
    return result
  }, {})
}

if this returns null, we'll get an error when doing Object.keys(null) in getDerivedStateFromProps

static getDerivedStateFromProps(props, state) {
    const query = getQuery(props)
    if (!query) throw new Error('Invalid or missing MediaQuery!')
    const values = getValues(props)
    if (query === state.query && values === state.values) return null // nothing changed
    const forceStatic = Object.keys(values).length !== 0
    const mq = matchMedia(props, query, forceStatic)
    return {
      matches: mq.matches,
      mq,
      query,
      values
    }
  }

@yocontra
Copy link
Owner

@Herdismaria Pushed a fix.

@s-diez
Copy link

s-diez commented Feb 26, 2019

I had the same problem as @Herdismaria and tried the suggested branch. I does contain one error but seems to work otherwise.

The error:

https://github.com/contra/react-responsive/compare/static-fix#diff-1fdf421c05c1140f6d71444ea2b27638R48

matchMedia() has to be called with matchMedia(query, props, !!values) instead of matchMedia(props, query, !!values).

@yocontra
Copy link
Owner

@s-diez Pushed

There is a test failure on this branch though, would need to be fixed before merging

@molcik
Copy link

molcik commented Apr 8, 2019

Any progress on this?

@yocontra
Copy link
Owner

yocontra commented Apr 8, 2019

@molcik Just waiting on somebody to test the static-fix branch and confirm it is fixed

@yocontra
Copy link
Owner

@molcik If you're having the same issue, can you test the branch?

yocontra added a commit that referenced this issue Apr 29, 2019
@yocontra
Copy link
Owner

Published a fix as 6.1.2 - please test and let me know if it is still an issue and I can reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants