在 React 中使用 window.location.reload 是否正确?

当您打开reactjs.org 时,在“Declarative”标题下,有一句话:当您的数据发生变化时,React 将有效地更新和呈现正确的组件。


对于我的几个应用程序,我使用以下结构:App | AppContainer(所有应用逻辑,登录前保护)| 登录(登录表单)


如果您render根据用户的凭据在 App's 中返回 2 个不同的组件,则此结构很有效。


render(){

   if(isUserLoggedIn()){

       return <AppContainer />;

   }


   return <Login />;

}

在Login组件内部,我正在刷新页面,window.location.reload以便render触发应用程序,然后我将获取AppContainer组件。


但是感觉有点像jQuery + Angular。是否有更好的(更多 React)方式来触发渲染功能,或者这应该是怎样的?


守候你守候我
浏览 408回答 2
2回答

宝慕林4294392

有没有更好(更多反应)的方式来触发渲染功能......通常的方法是拥有状态,在这种情况下,至少是用户是否登录的布尔值,并在用户成功登录或注销时更新该状态。更新状态会触发渲染。就您而言,由于您使用的是 Redux,因此您的状态可能会在那里。我不使用 Redux(还没有?),这大概是没有的样子,粗略地(如果您使用的是类组件,就像您看起来的那样):class App extends Component {&nbsp; &nbsp; constructor(props) {&nbsp; &nbsp; &nbsp; &nbsp; super(props);&nbsp; &nbsp; &nbsp; &nbsp; this.state = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loggedIn: /*...initial value, perhaps from web storage or cookie...*/;&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; this.onLogin = this.onLogin.bind(this);&nbsp; &nbsp; &nbsp; &nbsp; this.onLogout = this.onLogout.bind(this);&nbsp; &nbsp; }&nbsp; &nbsp; onLogin() {&nbsp; &nbsp; &nbsp; &nbsp; // ...probably stuff here, then:&nbsp; &nbsp; &nbsp; &nbsp; this.setState({loggedIn: true});&nbsp; &nbsp; }&nbsp; &nbsp; onLogout() {&nbsp; &nbsp; &nbsp; &nbsp; // ...probably stuff here, then:&nbsp; &nbsp; &nbsp; &nbsp; this.setState({loggedIn: false});&nbsp; &nbsp; }&nbsp; &nbsp; render() {&nbsp; &nbsp; &nbsp; &nbsp; if (this.state.logedIn) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return <AppComponent onLogout={this.onLogout}/>;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return <Login onLogin={this.onLogin}/>;&nbsp; &nbsp; }}或带钩子:const App = () => {&nbsp; &nbsp; const [loggedIn, setLoggedIn] = useState(/*...initial value, perhaps from web storage or cookie...*/);&nbsp; &nbsp; const onLogin = useCallback(() => {&nbsp; &nbsp; &nbsp; &nbsp; // ...probably stuff here, then:&nbsp; &nbsp; &nbsp; &nbsp; setLoggedIn(true);&nbsp; &nbsp; }, [loggedIn]);&nbsp; &nbsp; const onLogout = useCallback(() => {&nbsp; &nbsp; &nbsp; &nbsp; // ...probably stuff here, then:&nbsp; &nbsp; &nbsp; &nbsp; setLoggedIn(false);&nbsp; &nbsp; }, [loggedIn]);&nbsp; &nbsp; if (this.state.logedIn) {&nbsp; &nbsp; &nbsp; &nbsp; return <AppComponent onLogout={onLogout}/>;&nbsp; &nbsp; }&nbsp; &nbsp; return <Login onLogin={onLogin}/>;}(再次,粗略)

郎朗坤

如果您需要更新组件状态,那么您可以传递一个 observable 并监听更改或使用一些状态管理库。这是一种可能的解决方案:创建可观察的类declare type IObserverHandler = (event: any) => void;export class Observable {&nbsp; &nbsp; private observers: IObserverHandler[] = [];&nbsp; &nbsp; public subscribe(observer: IObserverHandler) {&nbsp; &nbsp; &nbsp; &nbsp; if (!this.observers.includes(observer)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.observers.push(observer);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public unsubscribe(observer: IObserverHandler) {&nbsp; &nbsp; &nbsp; &nbsp; this.observers = this.observers.filter(o => o !== observer);&nbsp; &nbsp; }&nbsp; &nbsp; public publish(event: any) {&nbsp; &nbsp; &nbsp; &nbsp; for (const observer of this.observers) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; observer(event);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}创建 Login 类,该类将发布有关登录或注销等操作的事件class Login extends Observable {&nbsp; &nbsp; public login() {&nbsp; &nbsp; &nbsp; &nbsp; this.publish({ value: true });&nbsp; &nbsp; }&nbsp; &nbsp; public logout() {&nbsp; &nbsp; &nbsp; &nbsp; this.publish({ value: false });&nbsp; &nbsp; }}在组件中订阅观察者并使用事件值更新组件状态export abstract class Component extends React.Component<any, any> {&nbsp; &nbsp; private observer: IObserverHandler;&nbsp; &nbsp; private observable: Login;&nbsp; &nbsp; constructor(props: any) {&nbsp; &nbsp; &nbsp; &nbsp; super(props);&nbsp; &nbsp; &nbsp; &nbsp; this.observable = this.props.observable;&nbsp; &nbsp; &nbsp; &nbsp; this.state = { isAuthenticated: false }&nbsp; &nbsp; &nbsp; &nbsp; this.observer = (event) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.setState({ isAuthenticated: event.value })&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; this.observable.subscribe(this.observer);&nbsp; &nbsp; }&nbsp; &nbsp; componentWillUnmount() {&nbsp; &nbsp; &nbsp; &nbsp; this.observable.unsubscribe(this.observer);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript