Javascript React钩子:隐藏/显示来自其他组件的元素
我有一个具有全局导航的项目。导航内部有一个按钮,该按钮应该能够隐藏具有“en”类的某些元素 在Javascript React钩子:隐藏/显示来自其他组件的元素,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,我有一个具有全局导航的项目。导航内部有一个按钮,该按钮应该能够隐藏具有“en”类的某些元素 在nav.js文件中 这是按钮的代码 <Button id="switch-lang" onClick={translate}>ENGLISH</Button> 在context.js中 export const SwitchLangContext = createContext(); const [switchedLang, setSwitchLang = useS
nav.js
文件中
这是按钮的代码
<Button id="switch-lang" onClick={translate}>ENGLISH</Button>
在context.js中
export const SwitchLangContext = createContext();
const [switchedLang, setSwitchLang = useState(true);
const context = useContext(SwitchLangContext);
在我的homecontent.js中
export const SwitchLangContext = createContext();
const [switchedLang, setSwitchLang = useState(true);
const context = useContext(SwitchLangContext);
这应该是我想要隐藏的div
<div className="en">
This is my planned content
</div>
这是我计划的内容
我很不确定如何在React钩子中隐藏类为“en”的元素。我做过类似的事情,但都在同一个文件中。现在,这些都在两个不同的文件中,我很不确定如何隐藏。一种基于道具隐藏元素的方法是这样做:
const MyComponent = (props) => {
const contentNode = null;
if (props.switchedLang === true) {
contentNode = (
<div className="en">
This is my planned content
</div>
);
}
return (
<React.Fragment>
{contentNode}
</React.Fragment>
);
}
然后
这是我计划的内容
...
隐藏基于道具的元素的一种方法是这样做:
const MyComponent = (props) => {
const contentNode = null;
if (props.switchedLang === true) {
contentNode = (
<div className="en">
This is my planned content
</div>
);
}
return (
<React.Fragment>
{contentNode}
</React.Fragment>
);
}
然后
这是我计划的内容
...