参考错误:导航器未使用 nextjs 定义 [英] Reference Error:Navigator not defined with nextjs
本文介绍了参考错误:导航器未使用 nextjs 定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码是这样的:
导入反应,{ useEffect } from 'react';
import React, { useEffect } from 'react';
import alanBtn from '@alan-ai/alan-sdk-web';
const alanKey = my key;
const App = () => {
useEffect(() => {
alanBtn({
key: alanKey,
onCommand: ({ command }) => {
alert('This code was executed');
}
})
}, []);
return (
<div><h1>Alan AI News Application</h1></div>);
}
export default App;
但我得到的错误是:参考错误:导航器未定义..如何解决?
But i am getting the error as: Reference Error:Navigator not defined.. How to fix it?
推荐答案
window
、navigator
等浏览器对象应该在useEffect
中定义使用前先.
Browser objects like window
, navigator
etc should be define in useEffect
first before use.
const [pageURL, setPageURL] = useState("");
const [isNativeShare, setNativeShare] = useState(false);
useEffect(() => {
setPageURL(window.location.href);
if (navigator.share) {
setNativeShare(true);
}
}, []);
// Now, use can use pageURL , isNativeShare in code
这篇关于参考错误:导航器未使用 nextjs 定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文