내가 사용 "창이 정의되지 않은"MERN - https://github.com/Hashnode/mern-starter (반응 REDUX, 웹팩, nodejs, 표현) 및 구성 요소 반응 사운드 - https://github.com/leoasis/react-sound웹팩 렌더링 :
내가 포함 구성 요소
import Sound from 'react-sound';
그리고 서버를 시작하려고하면 webpack 서버 렌더링에서 "window is not defined"
오류가 발생합니다.
하지만이 줄에 주석을 달고 서버를 시작하면 모두 문제가되지 않습니다. 서버가 실행 중일 때 변경 사항은 서버 렌더링 (전면 렌더링 만)을 트리거하지 않기 때문에이 줄과 구성 요소의 주석을 제거 할 수 있습니다.
내가
if (typeof window !== 'undefined') {
const Sound = require('react-sound');
}
을 시도하고에 내가 (웹팩 후) 전면 렌더링 ReferenceError: Sound is not defined
오류가
render() {
return (
<div>
<Sound playStatus={Sound.status.STOPPED} url="" />
</div>
);
}
렌더링합니다.
UPDATE :
나는 전면 렌더링 TypeError: Cannot read property 'status' of undefined
오류가 (var
하지 const
)
if (typeof window !== 'undefined') {
var Sound = require('react-sound');
}
시도하십시오.
안쪽 CONST 사운드를 정의 : 다음
용액 일 수있다. 가져 오기 var Sound = null; if의 바깥 쪽에서 if 값을 설정합니다. – scrappedcola