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

useLayoutEffect warning when server side rendering #317

Closed
derekr opened this issue Oct 25, 2021 · 3 comments · Fixed by #318
Closed

useLayoutEffect warning when server side rendering #317

derekr opened this issue Oct 25, 2021 · 3 comments · Fixed by #318
Labels
Beginner Friendly Approachable to contributors of all levels

Comments

@derekr
Copy link

derekr commented Oct 25, 2021

Hey! In a NextJS project when consuming useField I get a warning about useLayoutEffect:

Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client. See https://reactjs.org/link/uselayouteffect-ssr for common fixes.

Thoughts on switching to useEffect or is there another workaround that doesn't involve only rendering fields on the client? Thanks!

Also incase it's helpful here is the component I'm rendering:

const RadioButton = ({ label, value, ...props }) => {
  return (
    <>
      <label>
        <HiddenRadio {...props} value={value} />
        <span className="block text-center text-gray-700 py-4 px-6 rounded-3xl md:leading-none cursor-pointer border-2 border-gray-700 hover:bg-gray-700 hover:text-gray-200 peer-checked:text-gray-200 peer-checked:bg-gray-700 peer-checked:cursor-default">
          {label}
        </span>
      </label>
    </>
  )
}

const AttendingField = () => {
  const [attendingProps, _attendingMeta] = useField({
    name: 'attending',
    initialValue: ''
  })

  return (
    <div className="flex justify-center gap-2">
      <RadioButton
        {...attendingProps}
        label="Joyfully accept"
        value="yes"
        checked={attendingProps.value === 'yes'}
      />
      <RadioButton
        {...attendingProps}
        label="Regretfully decline"
        value="no"
        checked={attendingProps.value === 'no'}
      />
    </div>
  )
}
@andyrichardson andyrichardson added the Beginner Friendly Approachable to contributors of all levels label Oct 27, 2021
@andyrichardson
Copy link
Owner

Hey @derekr thanks for the report 👍

If you have a moment, give fielder@2.1.1-beta.0 a shot and let me know if that's working as expected!

@derekr
Copy link
Author

derekr commented Oct 27, 2021

That did the trick! Thanks for the update :).

@andyrichardson
Copy link
Owner

@derekr thanks for testing that out, this is now resolved in v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Beginner Friendly Approachable to contributors of all levels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants