简体   繁体   中英

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? 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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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