即使状态钩子称为

时间:2020-08-31 21:43:45

标签: reactjs react-hooks state

在此功能中:

import React, { useEffect } from 'react';
import { BrowserRouter, Route, Link } from 'react-router-dom'
import HomeScreen from  './Screens/HomeScreen'
import './App.css';
import currentStrings from './utils/currentlang'

function App() {

  const [currentlang, setCurrentStrings] = React.useState(currentStrings)
  return (
    <BrowserRouter>
       <div className="grid-container">
        <header className="header">
          <div className="brand">
            <Link to="/" >
                
            </Link>
          </div>
          <div className="header-side">
            {currentlang.getCurrent.subtitle}
          </div>
          <div className="header-right">
            <button  onClick={() => setCurrentStrings(currentlang.switchLang())}> {currentlang.getCurrent.traduction} </button>
          </div>
          <div>

          </div>
        </header>
        <main className="main">
          <div className="content">
            <Route path="/" exact={true} component={HomeScreen} />
          </div>

        </main>
        <footer className="footer">
          &#169; 2020 
         </footer>
      </div>
    </BrowserRouter>
   
  );
}

export default App;

我使用按钮(<button onClick={() => setCurrentStrings(currentlang.switchLang())}>)更改对象currentStrings(或用于useState挂钩的currentLang)的属性之一

但是我的功能似乎没有重新呈现,我可以确认这一点,因为当单击按钮时,我的日志确实告诉对象确实更改了其属性,但是屏幕没有显示它。

import {strings as frstrings} from '../res/lang/fr/strings'
import {strings as engstrings} from '../res/lang/eng/strings'

class CurentLang {
    constructor(){
        this.current = engstrings;    
    }
    switchLang() {
        if(this.current === frstrings){
            this.current = engstrings;
            console.log("engstrings");
        } else{
            this.current = frstrings;
            console.log("frstrings");
        }
        return new this.CurentLang;
    }
    get getCurrent(){
        return this.current;
    }

}

var currentStrings = new CurentLang()

export default currentStrings;

0 个答案:

没有答案