简体   繁体   English

如果 React 中的变量发生变化,如何重新渲染组件(使用 Hooks)

[英]How to re-render a component if a variable changes in React (with Hooks)

(Edit: I found something else wrong with the code and fixed it. So this question becomes irrelevant.) (编辑:我发现代码还有其他问题并修复了它。所以这个问题变得无关紧要。)

How to re-render the MyComponent component when const language changes?const language更改时,如何重新渲染MyComponent组件? Now it works only when I switch the tabs, but I need it working when the language is changed in another component.现在它仅在我切换选项卡时才有效,但是当另一个组件中的语言更改时,我需要它工作。

import React from "react";
import {useSelector} from "react-redux";

export default function MyComponent(props: any) {
    const language: string = useSelector((state: any) => state.config.language);
......
}

Like said, if you are using redux properly it should alrealdy be re-rendering...就像说的,如果你正确使用 redux,它应该已经重新渲染......

But if it dont, in a last case, you can force a re-render in the component by setting a key in parent div and changing its values whenever it config languague changes.但是,如果没有,在最后一种情况下,您可以通过在父 div 中设置一个键并在配置语言更改时更改其值来强制重新渲染组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM