首页 > 解决方案 > reactjs 改变 url 但不渲染任何东西

问题描述

我正在尝试在单击按钮后呈现组件。我正在为我的项目使用 React-Redux。单击按钮时,它确实更改了 url,但没有呈现任何内容。我什至在路由器内的 app.js 中提到了我的路由。代码如下。

应用程序.js

import React, { Component } from "react";
import {
  BrowserRouter as Router,
  Route,
  Switch,
  Redirect,
  withRouter
} from "react-router-dom";
import "./App.scss";
import PrivateRoute from "./PrivateRoute";
import { GlobalHistory } from "./_helpers/history";
import { Spinner } from "reactstrap";
import { userService } from "./_services/user.service";
import ProductDetails from "./website/dashboard/product-details/ProductDetails";
import Profile from "./website/dashboard/profile";
import DocRegister from "./views/Public/Login/docregister.jsx";
import { DoctorProductLocation } from "./locations";
import ContainingResult from "./website/dashboard/search/containing-results";

const loading = () => <Spinner color="success" />;
const DefaultLayout = React.lazy(() => import("./containers/DefaultLayout"));

// Pages
// const LandingPage = React.lazy(() => import('./views/Public/Landing/LandingPage'));
const Confirmation = React.lazy(() =>
  import("./views/Public/Register/Confirmation")
);
const SearchResult = React.lazy(() =>
  import("./views/Public/Search/SearchResult/SearchResult")
);
const Login = React.lazy(() => import("./views/Public/Login"));
const Logout = React.lazy(() => import("./views/Public/Login/Logout"));
const Register = React.lazy(() => import("./views/Public/Register/Register"));
const Page404 = React.lazy(() => import("./views/Public/Page404"));
const Page500 = React.lazy(() => import("./views/Public/Page500"));

// const Login = React.lazy(() => import('./views/Public/authentication/holder'));

class App extends Component {
  render() {
    return (
      <Router>
        <React.Suspense fallback={loading()}>
          <GlobalHistory />
          <Switch>
            <Route
              exact
              path="/login"
              render={props =>
                userService.isLoggedIn() ? (
                  <Redirect to="/" />
                ) : (
                  <Login {...props} />
                )
              }
            />
            <Route
              exact
              path="/logout"
              name="Logout Page"
              render={props => <Logout {...props} />}
            />
            {/* <Route exact path="/" name="Landing Page" render={props => <LandingPage {...props}/>} /> */}
            <Route
              exact
              path="/confirmation"
              render={props => <Confirmation {...props} />}
            />
            <Route
              exact
              path="/search-result"
              name="Search Result Page"
              render={props => <SearchResult {...props} />}
            />
            <Route
              exact
              path="/register"
              name="Register Page"
              render={props => <Register {...props} />}
            />
            <Route
              exact
              path="/404"
              name="Page 404"
              render={props => <Page404 {...props} />}
            />
            <Route
              exact
              path="/500"
              name="Page 500"
              render={props => <Page500 {...props} />}
            />
            <Route
              exact
              path="/dashboard/product/details"
              name="productDetails"
              render={props => <ProductDetails {...props} />}
            />
            <PrivateRoute
              path="/dashboard"
              name="Home"
              component={DefaultLayout}
            />
            <Route exact path="/profile" name="profile" component={Profile} />


//This is the route
            <Route
              exact
              path="/resultLoader"
              name="resultLoader"
              component={ContainingResult}
            />

            <Route
              exact
              path="/docregister"
              name="docregister"
              component={DocRegister}
            />

            <Redirect to="/dashboard" />
          </Switch>
        </React.Suspense>
      </Router>
    );
  }
}

export default App;

包含结果--组件

import React from "react";

import { connect } from "react-redux";
import SingleSearchResult from "./single-search-result";
import { results } from "../../../_actions/data/result";

import "react-loader-spinner/dist/loader/css/react-spinner-loader.css";
import Loader from "react-loader-spinner";
import { useSelector, useDispatch } from "react-redux";
import { paginate } from "../../../_actions/search.action";

import Pagination from "./pagination";
import "./test.css";
import SearchResult from "../../../views/Public/Search/SearchResult/SearchResult";
import SearchBar from "./search-bar";
import NavBar from "../navBar";
import Footer from "../footer";
import { fetchProducts, searchProduct } from "../../../_actions/search.action";

import Box from "@material-ui/core/Box";
// import SearchBar from "./search-bar";

class ContainingResult extends React.Component {
  render() {
    const { loading } = this.props;
    const str = this.props.location.search;
    const res = str.split("?q=");
    return (
      <React.Fragment>
        <NavBar></NavBar>
        <SearchBar></SearchBar>
        <div className="boxResult">
          <h1 style={{ color: "black" }}>
            {res}
            <hr style={{ backgroundColor: "black", width: "25%" }}></hr>
          </h1>
          <p style={{ color: "black" }}>Here is the teasing text...</p>
          <button
            className="btn btn-success"
            style={{ float: "right", backgroundColor: "#2d2762 " }}
          >
            See More
          </button>
        </div>
        <Footer></Footer>
      </React.Fragment>
    );
  }
}

const mapStateToProps = state => ({
  loading: state.product.loading
});

export default connect(mapStateToProps, { searchProduct, fetchProducts })(
  ContainingResult
);
// export default ContainingResult;

搜索栏按钮组件点击后重定向

import React, { Component } from "react";
import React, { Component } from "react";
import { connect } from "react-redux";
import { Link, Route, Router } from "react-router-dom";
import {
  searchProduct,
  fetchProducts,
  setLoading,
  saveKeyword,
  filterProducts
} from "../../../_actions/search.action";
import Loader from "react-loader-spinner";
import { Collapse } from "reactstrap";
import Footer from "../footer";
import SingleSearch from "./single-search-result";

import { resultLoader } from "../../../locations";
class SearchBar extends Component {
  handleInputChange = e => {
    e.preventDefault();
    this.props.searchProduct(e.target.value);
  };

  onClick = e => {
    console.log("I am " + this.props.text);
    console.log("Search button clicked");
    this.props.saveKeyword(this.props.text);
    this.props.fetchProducts(this.props.text);
    // this.props.setLoading();
  };

  onClickFilter = e => {
    this.props.filterProducts(e.target.value);
  };

  render() {
    const { text, loading, filter } = this.props;

    return (
      <div>
        <div className="s130">
          <form
            onSubmit={e => {
              e.preventDefault();
            }}
          >
            <div className="inner-form">
              <div className="input-field first-wrap">
                <div className="svg-wrapper">
                  <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="auto"
                    height="auto"
                    viewBox="0 0 24 24"
                  >
                    <path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z">
                      {" "}
                    </path>
                  </svg>
                </div>
                <input
                  id="search"
                  type="text"
                  value={text}
                  placeholder="What are you looking for?"
                  onChange={this.handleInputChange}
                  ref={input => (this.textInput = input)}
                />
              </div>
              <div className="input-field second-wrap">
                <Link to={"/resultLoader"}>
                  <button
                    className="btn-search"
                    type="button"
                    onClick={this.onClick}
                  >
                    {" "}
                    SEARCH{" "}
                  </button>
                </Link>
              </div>
              {/* {`/dashboard/search?q=${text}`} */}

              {loading ? "Searching" : ""}

              {this.pr}
            </div>
          </form>
        </div>
        <br></br>
        <label className="" style={{ marginLeft: "30%" }}>
          <input
            type="radio"
            value="External"
            name="radio"
            onClick={this.onClickFilter}
          ></input>
          &nbsp; Web Search
        </label>

        <label className="" style={{ marginLeft: "5%" }}>
          <input
            type="radio"
            value="Internal"
            name="radio"
            onClick={this.onClickFilter}
          ></input>
          &nbsp; Product Search
        </label>
      </div>
    );
  }
}

const mapStateToProps = state => ({
  text: state.product.text,
  loading: state.product.loading,
  filter: state.product.filter
});

export default connect(mapStateToProps, {
  searchProduct,
  fetchProducts,
  setLoading,
  saveKeyword,
  filterProducts
})(SearchBar);


标签: javascriptreactjsredux

解决方案


看来您显然正在包装Link它将转换为具有自己处理程序的a标记。buttononClick

您应该删除Link并使用历史 API 并使用按钮的history.push内部onClick方法,因为某些东西很可能不会执行您的按钮的 onClick 函数。

https://github.com/ReactTraining/history


推荐阅读