简体   繁体   English

在React中更新状态后清除DOM元素

[英]Clear DOM element after updating state in React

Hello I am trying to implement an auth logic to my page. 您好,我正在尝试对页面执行身份验证逻辑。 The issue I am having, after updating the state of my page, it doesn't remove the authenticated stuffs. 我遇到的问题是,更新页面状态后,它没有删除经过身份验证的内容。 How, do I clear the elements? 如何清除元素?

I am trying to use a ternary operator, it does work. 我正在尝试使用三元运算符,它确实可以工作。 However it doesn't update when the user login or logout. 但是,当用户登录或注销时,它不会更新。 It only renders one time, and it just sits there afterwards. 它只渲染一次,然后再坐在那里。

header.js header.js

import React, { Component } from 'react';

import LoginHeader from './auth_logic/login_header';
import PublicHeader from './auth_logic/public_header';

export default class Header extends Component {
  constructor(props) {
    super(props);
    this.state = {isAuth: false};
  }

  render() {
    if (Meteor.userId) {
      this.setState({isAuth: !this.state.isAuth});
    }
    else {
      this.setState({isAuth: !this.state.isAuth});
    }

    return (
      <div>
        {this.state.isAuth ? <PublicHeader /> : <LoginHeader />}
      </div>
    );
  }
}

Here is my LoginHeader, when the user is authenticated it will show additional information, and navigation authenticated links. 这是我的LoginHeader,当用户通过身份验证时,它将显示其他信息,以及导航身份验证的链接。

auth_logic/login_header.js auth_logic / login_header.js

import React, { Component } from 'react';

import Accounts from '../accounts';

export default class LoginHeader extends Component {
  render() {
    return (
      <div className="nav navbar-default">
        <a className="navbar-brand">Nick Reviews</a>

        <ul className="nav navbar-nav">
          <li>
            <a href="#">
              To Watch
              <span className="badge margin-left-3px">0</span>
            </a>
          </li>
          <li>
            <a href="#">
              Reviews
              <span className="badge margin-left-3px">0</span>
            </a>
          </li>
          <li>
            <Accounts />
          </li>
          <li><a href="#"> Create new item </a></li>
        </ul>
      </div>
    );
  }
}

It is exactly the same as the login_header.js file, but with less information. 它与login_header.js文件完全相同,但是信息较少。

auth_logic/public_header.js auth_logic / public_header.js

import React, { Component } from 'react';

import Accounts from '../accounts';

export default class PublicHeader extends Component {
  render() {
    return (
      <div className="nav navbar-default">
        <a className="navbar-brand">Nick Reviews</a>

        <ul className="nav navbar-nav">
          <li>
            <a href="#">
              To Watch
              <span className="badge margin-left-3px">0</span>
            </a>
          </li>
          <li>
            <a href="#">
              Reviews
              <span className="badge margin-left-3px">0</span>
            </a>
          </li>
          <li>
            <Accounts />
          </li>
        </ul>
      </div>
    );
  }
}

Assuming that your userId is a prop from above, use componentWillReceiveProps() react life cycle method to update your state depends on your props. 假设您的userId是上面的道具,请使用componentWillReceiveProps()反应生命周期方法来更新状态,具体取决于您的道具。 for more info refer this link - https://reactjs.org/docs/react-component.html 有关更多信息,请参阅此链接-https://reactjs.org/docs/react-component.html

标头组件不应在内部保持isAuth状态,它应来自容器组件,例如Home / About(Router Page)...根据prop更改呈现条件。

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

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