简体   繁体   English

如何理解 React 文件

[英]How to understand a React file

I am using React in Laravel and I found a problem that I can't refresh or reload a page in React.我在 Laravel 中使用 React,我发现我无法在 React 中刷新或重新加载页面的问题。 So to solve this problem I found many suggestions like use historyApiFallback , 404 page and many other ways But I found none of them useful to me now.所以为了解决这个问题,我找到了很多建议,比如使用historyApiFallback404 page和许多其他方法,但我发现它们现在对我没有用。

I know I can't do this because React has no system for it because of server- and client-side routing .我知道我不能这样做,因为 React 由于server- and client-side routing 而没有系统。 Then i found a demo project where they used Redux and I can refresh their page.然后我找到了一个演示项目,他们使用了 Redux,我可以刷新他们的页面。 I got the demo project where i can use any component and refresh them how many times I want.我得到了演示项目,我可以在其中使用任何组件并刷新它们多少次。 So there is a file name with Base.js and I am not understanding this file why he used it what is doing.所以有一个带有Base.js的文件名,我不明白这个文件为什么他用它来做什么。 Let me share the file and where it was used.让我分享文件以及它的使用位置。

Base.js Base.js

import React from 'react';
import { connect } from 'react-redux';
import Header from './components/Header';

const Base = ({ children }) => (
  <div>
    <Header />
    <main>{children}</main>
  </div>
);

const mapStateToProps = (state) => ({
  isAuthenticated: state.Auth.isAuthenticated,
});

export default connect(mapStateToProps)(Base);

Header.Js Header.Js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import {
  Nav,
  NavItem,
  NavLink,
  UncontrolledDropdown,
  DropdownToggle,
  DropdownMenu,
  DropdownItem,
} from 'reactstrap';
import * as actions from '../store/actions';

class Header extends Component {
  handleLogout = (e) => {
    e.preventDefault();
    this.props.dispatch(actions.authLogout());
  };

  render() {
    return (
      <header className="d-flex align-items-center justify-content-between">
        <h1 className="logo my-0 font-weight-normal h4">
          <Link to="/">Laravel React</Link>
        </h1>

        {this.props.isAuthenticated && (
          <div className="navigation d-flex justify-content-end">
            <Nav>
              <NavItem>
                <NavLink tag={Link} to="/archive">
                  Archive
                </NavLink>
                <NavLink tag={Link} to="/Myfile">
                Myfile
                </NavLink>
              </NavItem>
              <UncontrolledDropdown nav inNavbar>
                <DropdownToggle nav caret>
                  Account
                </DropdownToggle>
                <DropdownMenu right>
                  <DropdownItem>Settings</DropdownItem>
                  <DropdownItem divider />
                  <DropdownItem onClick={this.handleLogout}>
                    Log Out
                  </DropdownItem>
                </DropdownMenu>
              </UncontrolledDropdown>
            </Nav>
          </div>
        )}
      </header>
    );
  }
}

const mapStateToProps = (state) => ({
  isAuthenticated: state.Auth.isAuthenticated,
});

export default connect(mapStateToProps)(Header);

Public.js公共.js

import PropTypes from 'prop-types';
import { Route } from 'react-router';
import Base from '../Base';

const PublicRoute = ({ component: Component, ...rest }) => (
  <Route
    {...rest}
    render={(props) => (
      <Base>
        <Component {...props} />
      </Base>
    )}
  />
);

PublicRoute.propTypes = {};

export default PublicRoute;

split.js split.js

import React from 'react';
import PropTypes from 'prop-types';
import { Route } from 'react-router';
import { connect } from 'react-redux';
import Base from '../Base';

const SplitRoute = ({
  component: Component,
  fallback: Fallback,
  isAuthenticated,
  ...rest
}) => (
  <Route
    {...rest}
    render={(props) => (isAuthenticated ? (
      <Base>
        <Component {...props} />
      </Base>
    ) : (
      <Base>
        < Fallback {...props} />
      </Base>
    ))}
  />
);

SplitRoute.propTypes = {
  isAuthenticated: PropTypes.bool.isRequired,
};

const mapStateToProps = (state) => ({
  isAuthenticated: state.Auth.isAuthenticated,
});

export default connect(mapStateToProps)(SplitRoute);

Now it has authenticated system so I understand it but why it is using base function and what it is doing?现在它有经过身份验证的系统,所以我理解它,但为什么它使用基础 function以及它在做什么? I am not understanding.我不理解。

What it looks like is that the Base.js is a container for the Header and any rendered children (passed props).看起来 Base.js 是 Header 和任何渲染的子项(传递的道具)的容器。 This is a good practise in react to separate logic and make it more readable.这是对单独的逻辑做出反应并使其更具可读性的好习惯。 So when he imports Base into the Public.js file, it will render the Header and the component he is passing to it from the public function props.因此,当他将 Base 导入 Public.js 文件时,它将呈现 Header 和他从公共 function 道具传递给它的组件。

Think of it like the skeleton of the layout, by importing Base it will always render the header and any logic inside of the header file, and whatever he is passing down to it.把它想象成布局的骨架,通过导入 Base 它将始终呈现 header 和 header 文件中的任何逻辑,以及他传递给它的任何内容。 As you can see he is passing different components to it depending on whether isAuthenticated is true or false.正如你所看到的,他根据 isAuthenticated 是真还是假向它传递不同的组件。 If it is false, they are rendering Base and passing a fallback component - this will render inside of the main tag within the Base function.如果它是假的,他们正在渲染 Base 并传递一个后备组件 - 这将在 Base function 内的主标签内渲染。

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

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