React上下文在Netlify中不起作用。 TypeError:无法读取未定义的属性
每次将 React 应用程序部署到 Netlify 时,我都会遇到此问题:
该代码在本地运行,但是一旦部署到 Netlify,就会生成上述错误。
这是我的代码:
import { createContext } from "react";
import { languageOptions, dictionaryList } from "../languages";
export const LanguageContext = createContext({
userLanguage: "hu",
dictionary: dictionaryList.hu,
});
export function LanguageProvider({ children }) {
const defaultLanguage = window.localStorage.getItem("rcml-lang");
const [userLanguage, setUserLanguage] = useState(defaultLanguage);
const provider = {
userLanguage,
dictionary: dictionaryList[userLanguage],
userLanguageChange: (selected) => {
const newLanguage = languageOptions[selected] ? selected : "hu";
setUserLanguage(newLanguage);
window.localStorage.setItem("rcml-lang", newLanguage);
},
};
return (
<LanguageContext.Provider value={provider}>
{children}
</LanguageContext.Provider>
);
}
export function Text({ tid }) {
const languageContext = useContext(LanguageContext);
return languageContext.dictionary[tid] || tid;
}
这是整个存储库的链接: https://github.com/fhonoria/ Thermal-delikat
最后是部署链接:https:// Thermaldelikat.netlify.app/
提前感谢大家!
I am having this issue every time I deploy my react application to Netlify:
The code works locally however, once deployed to Netlify it generates the above error.
Here is my Code:
import { createContext } from "react";
import { languageOptions, dictionaryList } from "../languages";
export const LanguageContext = createContext({
userLanguage: "hu",
dictionary: dictionaryList.hu,
});
export function LanguageProvider({ children }) {
const defaultLanguage = window.localStorage.getItem("rcml-lang");
const [userLanguage, setUserLanguage] = useState(defaultLanguage);
const provider = {
userLanguage,
dictionary: dictionaryList[userLanguage],
userLanguageChange: (selected) => {
const newLanguage = languageOptions[selected] ? selected : "hu";
setUserLanguage(newLanguage);
window.localStorage.setItem("rcml-lang", newLanguage);
},
};
return (
<LanguageContext.Provider value={provider}>
{children}
</LanguageContext.Provider>
);
}
export function Text({ tid }) {
const languageContext = useContext(LanguageContext);
return languageContext.dictionary[tid] || tid;
}
This is the link to the whole repository: https://github.com/fhonoria/Thermal-delikat
And finally the deploy link: https://thermaldelikat.netlify.app/
Thanks to all in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我而言,当我在下一个JS应用中使用themecontext以将光模式更改为“黑暗模式”时,我的网站没有被部署到Netlify中。
解决方案:我在Netlify中删除了整个代码。然后,我下载了我的github的所有previus代码(那是相反)。使用IT Agein之后,我的网站被部署在Netlify上。
我认为这是下一个JS 13.4版本错误,因此您无法解决它。午餐13.5后,问题(错误)将解决,请阅读下一个JS 13.5文档。在这里: https://nextjs.org/blog/blog/next-13-5
谢谢...
In my case, my site was not deployed in netlify when I use ThemeContext in my Next js app for change light mode to dark mode.
Solution: I remove the whole code in netlify. Then i was download all previus code (that was corrent) form my github. After I use it agein then my site was deploy on netlify.
I think, It's Next js 13.4 version error so you cna't solve it. After lunch 13.5 it's problem (bug) will be solved, Please read next js 13.5 documentation. in here: https://nextjs.org/blog/next-13-5
Thanks...