简体   繁体   English

如何在React中使用路由?

[英]How to use routes in React?

I created a web-app that uses reactjs. 我创建了一个使用reactjs的网络应用。 Now the basics of the app are : 现在,该应用程序的基础是:

Create Topics/Sub topics. 创建主题/子主题。 Display them (this uses react ) Now 2nd task is explained as :- 显示它们(这使用react)现在第二个任务解释为:-

Show all the topics/sub topics list. 显示所有主题/子主题列表。 Clicking any of them sends json req and the data is recieved and displayed. 单击它们中的任何一个将发送json req,并接收并显示数据。 Now I want to share the topic with link , however on clicking any of the topic only one elemnt of the html renders and the url remains the same. 现在,我想通过链接共享主题,但是单击任何主题时,只有一个html呈现器和url保持不变。

So this is what i want to do - 所以这就是我想做的-

On clicking , send json req and recieve data ( done ) Change the url ( I want to know if it is possible using the same react page? ) 单击时,发送json req和接收数据(完成)更改url(我想知道是否可以使用相同的react页面?)

How can I use Routes in the following code ? 如何在以下代码中使用Routes?

Part 1 :- Deals with recents posts Part 2 :- Deals with the Headings or Topic names Part 3 :- Deals with Sub topics Part 4 :- Deals with showing the Article from the chosen topic and sub topic. 第1部分:-处理最近发布的帖子第2部分:-处理标题或主题名称第3部分:-处理子主题第4部分:-处理显示所选主题和子主题的文章。

Now its obvious that though the article is displayed , the link / url of the page will never change. 现在很明显,尽管显示了文章,但是页面的链接/ URL永远不会改变。 However it makes it hard to share the articles. 但是,这使得共享文章变得困难。 Thus I want to know how to apply the routes in this case ? 因此,我想知道在这种情况下如何应用路线?

render() {
  var lists = this.state.list;
  var lists2 = this.state.list2;
  var aname = this.state.article.name;
  var date = this.state.article.date;
  var lists3 = this.state.listrec;

  return (
    <div className="row">
      <div id="recentsbar" className="col-md-3">
        <div className="row">
          <div className="col-md-9">
            <div>
              <div style={{ display: this.state.hide2 }}>
                <h2>
                  <b> Recents </b>
                </h2>
                <div
                  className="lds-ellipsis"
                  style={{ display: this.state.load2 }}
                >
                  <div />
                  <div />
                  <div />
                  <div />
                </div>
                <div id="subl">
                  {lists3.map(number => (
                    <Item3 name={number} show={this.rec.bind(this)} />
                  ))}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div className="col-md-8">
        <div style={{ display: this.state.hide }}>
          <h1 style={{ marginLeft: 6 + "em" }}>
            <b> Subjects List </b>
          </h1>
          <div className="row">
            <div className="col-md-6">
              <ul style={{ marginTop: 1 + "em" }} /*className="list-group"*/>
                <div
                  className="lds-ellipsis"
                  style={{ display: this.state.load }}
                >
                  <div />
                  <div />
                  <div />
                  <div />
                </div>
                {lists.map(number => (
                  <Item name={number} hide={this.hide.bind(this)} />
                ))}
              </ul>
            </div>
          </div>
        </div>
        <div className="col-md-8">
          <div style={{ display: this.state.show }}>
            <h1 style={{ marginLeft: 6 + "em" }}>
              <b> {this.state.selsub} </b>
            </h1>
            <div className="row">
              <div className="col-md-auto" style={{ marginLeft: 6 + "em" }}>
                <ul
                  style={{ marginTop: 1 + "em" }}
                  className="list-group art"
                >
                  <div
                    className="lds-ellipsis"
                    style={{ display: this.state.load }}
                  >
                    <div />
                    <div />
                    <div />
                    <div />
                  </div>
                  {lists2.map(number => (
                    <Item2 name={number} hide2={this.hide2.bind(this)} />
                  ))}
                </ul>
              </div>
            </div>
          </div>
        </div>

        <div style={{ display: this.state.showarticle }}>
          <div className="lds-ellipsis" style={{ display: this.state.load }}>
            <div />
            <div />
            <div />
            <div />
          </div>
          <div className="arthead"> {aname} </div>
          <div style={{ float: "right", opacity: 0.5 }}>
            Updated on: {date}{" "}
          </div>
          <div
            style={{
              marginLeft: 1 + "em",
              lineHeight: 2 + "em",
              marginTop: 4 + "em"
            }}
            dangerouslySetInnerHTML={{ __html: this.state.article.body }}
          />
        </div>
      </div>
    </div>
  );
}

You should look into react-router's Switch and react-router-dom's Route Components. 您应该查看react-router的Switch和react-router-dom的Route Components。 For instance the following jsx: 例如以下jsx:

import { Route, Link } from 'react-router-dom';
import { Switch } from 'react-router';
class YourComponent extends Component {
    render() {
        return (
            <Switch>
                <Route
                    path="/"
                    exact
                    render={
                        () => (
                           <h1>root content</h1>
                           <Link to="/content1">content1</Link>
                        )
                    }
                />
                <Route
                    path="/content1"
                    exact
                    render={
                        () => (
                           <h1>content 1</h1>
                           <Link to="/">root</Link>
                        )
                    }
                />
            </Switch>
        );
    }
}

Route allows conditional rendering based on the URL. Route允许基于URL进行条件渲染。

Switch ensures the first matching Route only will be rendered (actually what's returned by the render prop of the Route component). Switch确保仅将渲染第一个匹配的Route(实际上是Route组件的渲染道具返回的内容)。

For all of this to work, remember that a Router component from react router must be an ancestor of YourComponent. 为了使所有这些工作正常进行,请记住,来自React Router的Router组件必须是YourComponent的祖先。

If you want to trigger an SPA navigation after an event, or during a callback, you have to use the withRouter HOC from react-router. 如果要在事件之后或回调期间触发SPA导航,则必须使用react-router中的withRouter HOC。 StackOverflow thread explaining how it works: Programmatically navigate using react router StackOverflow线程解释其工作原理: 使用React Router以编程方式导航

I also suggest you look into react-router-redux for redux integration of react-router. 我还建议您研究react-router-redux以实现react-router的redux集成。 It offers the push action to navigate to another route. 它提供了push操作以导航到另一条路线。

References: 参考文献:

https://reacttraining.com/react-router/web/api/Switch https://reacttraining.com/react-router/web/api/Switch

https://reacttraining.com/react-router/web/api/Route https://reacttraining.com/react-router/web/api/Route

https://reacttraining.com/react-router/web/api/Link https://reacttraining.com/react-router/web/api/Link

https://reacttraining.com/react-router/web/api/withRouter https://reacttraining.com/react-router/web/api/withRouter

React : difference between <Route exact path="/" /> and <Route path="/" /> React:<Route精确路径=“ /” />和<Route path =“ /” />之间的区别

https://www.npmjs.com/package/react-router-redux https://www.npmjs.com/package/react-router-redux

I have created a Simple react routing example, hope it will make you undrestand routing in react. 我创建了一个简单的React路由示例,希望它会使您在React中无法理解路由。 https://github.com/shivakumaraswamy/reactjs/tree/master/ReacRouting https://github.com/shivakumaraswamy/reactjs/tree/master/ReacRouting

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

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