【问题标题】:I Get the data inside this.props.children;我获取 this.props.children 里面的数据;
【发布时间】:2020-07-03 06:22:21
【问题描述】:

我是 react 新手,我有一个很好的登录功能,但我需要使用 this.props.children 发送有关用户的下一页信息。我希望你能帮助我我的路线中的鳕鱼是下一个

<BrowserRouter>
        <Switch>
          <Route path="/" exact component={Carrousel} />
          <Route path="/registro" component={Register} exact/>
          <Route path="/login" component={Login} exact/>
          <Route path="/contact" component={Contact} exact/>
          <AuthComponent>
          <Route path="/protected" component={Protected} exact />
          </AuthComponent>
        </Switch>
      </BrowserRouter>

我在 authComponent 中的登录代码

import React, {Component} from 'react';
import axios from 'axios';
import { withRouter } from 'react-router-dom';
class AuhtComponent extends Component{
  constructor(props){
    super(props);

    this.state={
      user: undefined
    }
  }
  componentDidMount(){
   this.getUser();
  }
  getUser() {
    const jwt = getJwt();
    if (!jwt) {
      this.setState({
        user: null
      });
      return;
    }

    axios.get('getUser', { headers: { Authorization: `Bearer ${jwt}`} }).then(res => {
      this.setState({
        user: res.data
      })
    });
  }

  render(){
    const { user } = this.state;
    if (user === undefined) {
      return (
        <div>
          Loading...
        </div>
      );
    }

    if (user === null) {
      this.props.history.push('/login');
    }

    return this.props.children;
  }
}

export default withRouter(AuhtComponent);



现在我想恢复受保护的 nex 组件中的信息,但我

import React, {Component} from 'react';

class Inicio extends Component{
  render(){
    return(
        <h1>{this.state.user}</h1>
    )
  }
}

欢迎大家帮忙

【问题讨论】:

    标签: reactjs react-router react-router-v4 react-router-dom react-props


    【解决方案1】:

    您应该使用 HOC(高阶组件):

    工作版本链接:

    https://codesandbox.io/s/busy-booth-hwllz

    代码:

    const withAuth = WrappedComponent =>
      class extends React.Component {
        constructor(props){
        super(props);
    
        this.state={
          user: undefined
        }
      }
    
      componentDidMount(){
       this.getUser();
      }
    
      getUser() {
        const jwt = getJwt();
        if (!jwt) {
          this.setState({
            user: null
          });
          return;
        }
    
        axios.get('getUser', { headers: { Authorization: `Bearer ${jwt}`} }).then(res => {
          this.setState({
            user: res.data
          })
        });
      }
    
      render(){
        const { user } = this.state;
        if (user === undefined) {
          return (
            <div>
              Loading...
            </div>
          );
        }
    
        if (user === null) {
          this.props.history.push('/login');
        }
    
        return <WrappedComponent {...this.props} user={user}/>;
      }
      };
    
    export default withAuth;
    

    并像这样使用它:

    <BrowserRouter>
            <Switch>
              <Route path="/" exact component={Carrousel} />
              <Route path="/registro" component={Register} exact/>
              <Route path="/login" component={Login} exact/>
              <Route path="/contact" component={Contact} exact/>
              <Route path="/protected" component={withAuth(Protected)} exact />
            </Switch>
    </BrowserRouter>
    

    【讨论】:

    • 谢谢,但我有一个问题,我需要接收用户内部的信息,有什么建议可以在受保护的情况下显示信息吗?
    • 请检查提供的代码框链接,我正在通过用户那里!
    • 我修改了用户的回复。
    猜你喜欢
    • 2012-04-20
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    相关资源
    最近更新 更多