0

(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);
......
}
2
  • 2
    It should already be the case, if you are using redux properly. Commented Jul 15, 2021 at 12:51
  • 1
    Most likely you're not changing the state of language correctly through your actions or something is not quite setup on the reducer Commented Jul 15, 2021 at 13:44

1 Answer 1

2

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.

By request an example:

const [rerenderedComponent, setRerenderedComponent] = useState(1);

const forceUpdate = () => {
 return setRerenderedComponent(prev => prev + 1);
}

return (
<div onClick={forceUpdate} key={rerenderedComponent}>
...
</div>
)
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please give some exapmle?
I just edited my answer with an example...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.