0

我正在使用 apollo-boost 并且我创建了一个 ApolloClient 并且我想在运行突变后进行一些重定向但我无法访问道具

index.js

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter } from "react-router-dom";
import { ApolloProvider } from "react-apollo";
import ApolloClient from "apollo-boost";

import "./bootstrap";
import Routes from "./RoutesComponent";

const client = new ApolloClient({
  uri: "http://localhost:4000/graphql"
});

const Root = () => {
  return (
    <ApolloProvider client={client}>
      <BrowserRouter>
        <Routes />
      </BrowserRouter>
    </ApolloProvider>
  );
};

ReactDOM.render(<Root />, document.querySelector("#root"));

路由组件

import React, { Component } from "react";
import { Route, Switch } from "react-router-dom";

import Register from "./authenticate/register/RegisterComponent";
import Login from "./authenticate/login/LoginComponent";
import Product from "./customers/product/ProductComponent";
import AddProduct from "./customers/add-product/AddProductComponent";
import Products from "./customers/products/ProductsComponent";
import AdminProduct from "./admin/product/ProductComponent";
import AdminProducts from "./admin/products/ProductsComponent";

class RoutesComponent extends Component {
  render() {
    return (
      <Switch>
        <Route path="/products/new" render={() => <AddProduct />} />
      </Switch>
    );
  }
}
export default RoutesComponent;

添加产品组件

import React, { Component } from "react";

class AddProductComponent extends Component {
  render() {
    console.log(this.props);
    return (
      <div>
        <h1>AddProductComponent</h1>
      </div>
    );
  }
}
export default AddProductComponent;

控制台中的 this.props 显示{}

为了缓解这种情况,我还尝试使用 history 包,它更改了 url,但重定向不起作用

我可以使用状态进行重定向,但我宁愿使用历史道具而不是状态。

对此的任何建议将不胜感激。

4

1 回答 1

2

Routerender 函数props作为参数,因为你没有传递它,所以它在组件中不可用

<Route path="/products/new" render={() => <AddProduct />} />

固定的:

<Route path="/products/new" component={AddProduct} />
于 2019-02-04T13:51:42.420 回答