Caldis / react-zmage

一个基于 React 的可缩放图片控件 | A scalable image wrapper power by react
https://zmage.caldis.me
MIT License
920 stars 95 forks source link

视乎不支持ssr,服务端渲染报window is not defined #5

Open liumin1128 opened 6 years ago

liumin1128 commented 6 years ago

如题

Caldis commented 6 years ago

谢谢反馈,后续会修复这个问题

urnotzane commented 5 years ago

还没有支持ssr吗

himadrinath commented 5 years ago

@Caldis yes i tired but not supported yet. when will be supported?

repairearth commented 5 years ago
import React, {useRef} from 'react'
import ReactDOM from 'react-dom'
import useMount from 'react-use/lib/useMount'
import Zmage from 'react-zmage'

/**
 * Zmage 不支持 ssr(代码用到 window 和 document),gatsby 构建时会报错,所以包装一下,在 Mount 里面渲染
 * https://www.gatsbyjs.org/docs/debugging-html-builds/
 * @param {*} props
 */
export default function ZmageWrapper(props) {
  const wrapper = useRef(null)

  useMount(() => {
    ReactDOM.render(
      <Zmage {...props} />,
      wrapper.current
    )
  })

  return <div ref={wrapper} />
}

SSR 临时解决方案

hjtgzh commented 4 years ago

1、let CustomZmage 2、componentDidMount(){ CustomZmage=require('react-zmage') } 3、在render里面使用:<CustomZmage />

StinsonZhao commented 4 years ago

Thanks for your info. @repairearth

I use import and it works in gatsby:

import React, { useRef } from 'react'
import ReactDOM from 'react-dom'
import useMount from 'react-use/lib/useMount'

const ImageComp = (props) => {
  const imgwrapper = useRef(null)

  useMount(() => {
    import('react-zmage').then(({ default: Zmage }) => {
      ReactDOM.render(
        <Zmage {...props} />,
       imgwrapper.current
      )
    })
  })

  return <div ref={imgwrapper} />
}
noodv commented 4 years ago
import React, {useRef} from 'react'
import ReactDOM from 'react-dom'
import useMount from 'react-use/lib/useMount'
import Zmage from 'react-zmage'

/**
 * Zmage 不支持 ssr(代码用到 window 和 document),gatsby 构建时会报错,所以包装一下,在 Mount 里面渲染
 * https://www.gatsbyjs.org/docs/debugging-html-builds/
 * @param {*} props
 */
export default function ZmageWrapper(props) {
  const wrapper = useRef(null)

  useMount(() => {
    ReactDOM.render(
      <Zmage {...props} />,
      wrapper.current
    )
  })

  return <div ref={wrapper} />
}

SSR 临时解决方案

ReferenceError: window is not defined 大哥,还是报这个错误啊

hjtgzh commented 4 years ago
import React, {useRef} from 'react'
import ReactDOM from 'react-dom'
import useMount from 'react-use/lib/useMount'
import Zmage from 'react-zmage'

/**
 * Zmage 不支持 ssr(代码用到 window 和 document),gatsby 构建时会报错,所以包装一下,在 Mount 里面渲染
 * https://www.gatsbyjs.org/docs/debugging-html-builds/
 * @param {*} props
 */
export default function ZmageWrapper(props) {
  const wrapper = useRef(null)

  useMount(() => {
    ReactDOM.render(
      <Zmage {...props} />,
      wrapper.current
    )
  })

  return <div ref={wrapper} />
}

SSR 临时解决方案

ReferenceError: window is not defined 大哥,还是报这个错误啊

试下我上面写的那个方案