Uso MERN: https://github.com/Hashnode/mern-starter (react, redux, webpack, nodejs, express) y el componente react-sound: https://github.com/leoasis/react-sound
Cuando incluyo componente
import Sound from 'react-sound'; e intente iniciar el servidor, tengo el error "window is not defined" de la representación del servidor webpack.
Pero si comento esta línea e inicio el servidor, todo estará bien. Después de eso, puedo descomentar esta línea y el componente funcionará, porque cuando el servidor se está ejecutando, los cambios no activan la representación del servidor (solo la representación frontal).
si lo intento
if (typeof window !== 'undefined') { const Sound = require('react-sound'); }y en render
render() { return ( <div> <Sound playStatus={Sound.status.STOPPED} url="" /> </div> ); } Tengo ReferenceError: Sound is not defined error en la representación frontal (después del paquete web).
ACTUALIZAR:
Si intento ( var , no const )
if (typeof window !== 'undefined') { var Sound = require('react-sound'); } Tengo TypeError: Cannot read property 'status' of undefined en la representación frontal.
Parece que no puedes usar reaccionar-sonido fuera de un entorno de navegador.
La solución puede ser la siguiente:
let SoundEl; if (typeof window !== 'undefined') { import Sound from 'react-sound'; SoundEl = <Sound playStatus={Sound.status.STOPPED} url="" /> } else { SoundEl = <div></div> }...
render() { return ( <div> {SoundEl} </div> ) }