简体   繁体   English

如何通过单击 React 中的按钮来路由 class 组件?

[英]How to route class component by clicking button in React?

I am new to React.我是 React 的新手。 I am trying to build a page and having like 3 button or img on main page.我正在尝试构建一个页面并在主页上有 3 个buttonimg When I click either one, I shall be routed to another class component.当我单击其中一个时,我将被路由到另一个 class 组件。 You can treat it like click the icon and route you to another category page (just an example).您可以将其视为单击图标并将您路由到另一个类别页面(只是一个示例)。 Below is my structure and partial code I tried.以下是我尝试过的结构和部分代码。 I have no idea how to achieve that, and I googled and seems cannot find the stuff I want.我不知道如何实现这一点,我用谷歌搜索,似乎找不到我想要的东西。

Structure:结构:

/src
  .index.js
  .App.js
  .BookStore.js
  .FruitStore.js
  .FoodStore.js

index.js index.js

import React from "react";
import { render } from "react-dom";
import App from "./App";

render(
    <App />, 
    document.getElementById('root')
);

App.js:应用程序.js:

import React from "react";
import BookStore from "./BookStore";

const AppContainer = () => {
    return (
        //do the routing 
        <BookStore/>
    )
};

export default AppContainer;

BookStore.js书店.js

export default class BookStore extends React.Component {
}
const contentDiv = document.getElementById("root");
const gridProps = window.gridProps || {};
ReactDOM.render(React.createElement(BookStore , gridProps), contentDiv);

First, you could have a look at the/one react router, eg https://reactrouter.com/web/guides/quick-start首先,您可以查看/one react 路由器,例如https://reactrouter.com/web/guides/quick-start

However, since you're writing you're new to react, this might be a little too much...但是,由于您正在编写您的新反应,这可能有点太多了......

First, I was wondering why you're using the "ReactDOM" in your indexjs (that seems to be correct), but also in the BookStore.js.首先,我想知道为什么您在 indexjs 中使用“ReactDOM”(这似乎是正确的),但也在 BookStore.js 中。 I would also recommend to write your components as functions, like your "AppContainer" and not use the class components anymore (or do you really need to do that? - why?).我还建议将您的组件编写为函数,例如您的“AppContainer”,并且不再使用 class 组件(或者您真的需要这样做吗?-为什么?)。 You can use hooks instead to have eg state in the components.您可以使用钩子代替,例如 state 在组件中。

You would then need any kind of state in your AppContainer which is used for the routing.然后,您需要在用于路由的 AppContainer 中使用任何类型的 state。 Maybe like this:也许是这样的:

const AppContainer = () => {
    const [showDetail, setShowDetail] = useState();
    return <>
        {!showDetail && <BookStore onDetail={detail => setShowDetail(detail)} />}
        {showDetail && <DetailPage detail={showDetail} onBack={() => setShowDetail(undefined)}}
    </>
}

Your AppContainer then has a state wheter or not to show the Bookstore (which is shown when "showDetail" is falsy, or a DetailPage which is shown when showDetail is truthy.然后,您的 AppContainer 有一个 state 是否显示书店(当“showDetail”为假时显示,或者当 showDetail 为真时显示 DetailPage。

For this to work, your Bookstore needs to provide callbacks to let the AppContainer know that something should change.为此,您的 Bookstore 需要提供回调以让 AppContainer 知道应该更改某些内容。 Very simply it could look like this:很简单,它可能看起来像这样:

const BookStore = ({onDetail}) => {
    return <button onClick={() => onDetail("anything")}>Click me</button>
}

Now when someone clicks the button on the bookstore, it calls the "onDetail" callback, which was set in the AppContainer to set the "showDetail" state.现在当有人点击书店上的按钮时,它会调用“onDetail”回调,该回调是在AppContainer中设置的,用于设置“showDetail”state。 So this one will be updated to "anything" in this case.所以在这种情况下,这个将被更新为“任何东西”。 This will result in a rerender on the AppContainer which will now render a DetailPage component instead.这将导致 AppContainer 上的重新渲染,现在将渲染一个 DetailPage 组件。

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

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