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

fix ssr warning about useLayoutEffect #46

Merged
merged 1 commit into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './use-custom-tabs'
export * from './use-validators'
export * from './use-deep-effect'
export * from './use-deep-memo'
export * from './use-isomorphic-effect'
16 changes: 16 additions & 0 deletions src/hooks/use-isomorphic-effect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useEffect, useLayoutEffect } from 'react'

const isReactNative =
typeof global !== 'undefined' &&
global.navigator &&
global.navigator.product === 'ReactNative'

const isDOM = typeof document !== 'undefined'

/**
* Is `useLayoutEffect` in a DOM or React Native environment, otherwise resolves to useEffect
* Only useful to avoid the console warning.
*
* PREFER `useEffect` UNLESS YOU KNOW WHAT YOU ARE DOING.
*/
export const useIsomorphicEffect = isDOM || isReactNative ? useLayoutEffect : useEffect
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useLayoutEffect, forwardRef } from 'react'
import React, { forwardRef } from 'react'
import { lazy, Suspense } from '@uploadcare/client-suspense'
import { useIsomorphicEffect } from './hooks'

const Uploader = lazy(() =>
import(/* webpackChunkName: "ucare-widget-chunk" */ './uploader')
Expand All @@ -10,13 +11,13 @@ const Dialog = lazy(() =>
)

const Config = ({ locale, localeTranslations, localePluralize }) => {
useLayoutEffect(() => {
useIsomorphicEffect(() => {
window.UPLOADCARE_LIVE = false
window.UPLOADCARE_MANUAL_START = true
}, [])

// configurate translations
useLayoutEffect(() => {
useIsomorphicEffect(() => {
window.UPLOADCARE_LOCALE = window.UPLOADCARE_LOCALE || locale
window.UPLOADCARE_LOCALE_TRANSLATIONS =
window.UPLOADCARE_LOCALE_TRANSLATIONS || localeTranslations
Expand Down