简体   繁体   English

如何在React中创建页面

[英]How to create pages in react

I am creating a really simple app in react. 我正在创建一个非常简单的应用程序。 It will have a number of pages with minimum data on it. 它将包含许多页面,其中包含最少的数据。 I am looking for the simplest method to switch between the pages with as little complexities as possible. 我正在寻找最简单的方法,以尽可能少的复杂性在页面之间切换。

This is my current code: 这是我当前的代码:

render() {
    return (
        <div className="App test">
        <nav className="navbar pure-menu pure-menu-horizontal">
          <a href="#" className="pure-menu-heading pure-menu-link">
             Page 1
          </a>
          {
          }<ul className="pure-menu-list">
              <li className="pure-menu-item"><a href="#" className="pure-menu-link">Page 2</a></li>
              <li className="pure-menu-item"><a href="#" className="pure-menu-link">Page 3</a></li>
              <li className="pure-menu-item"><a href="#" className="pure-menu-link">Page 4</a></li>
              </ul>
          }
          </nav>
          <main className="container">
              <div className="pure-g">
                <div className="pure-u-1-1">
                  <p>
                    Page 1 text
                  </p>
                  <p>
                    Page 2 text
                  </p>
                  <p>
                    Page 3 text
                  </p>                      
                  <p>
                    Page 4 text
                  </p>

                </div>
              </div>
          </main>
        </div>);
  }
}

Is there a standard to switch between the content? 是否有在内容之间切换的标准? (Page text 1,2,3,4). (页面文本1,2,3,4)。

How simple? 有多简单? :). :)。 Simplest method possible would be to hold a pageNumber state in your component and only render a particular page when it is that number. 最简单的方法可能是在组件中保留pageNumber状态,并仅在该数字为特定页面时才呈现该页面。 Call setPageNumber with a parameter to see the component render different pages. 调用带有参数的setPageNumber以查看组件呈现不同的页面。

This is obviously not the best solution or scalable. 这显然不是最佳解决方案或可扩展性。

class App extends React.Component {
    constructor() {
        super();

        this.state = {
            pageNumber: 0
        };
    }
    onClickLink = (pageNumber) => {
        this.setState({ pageNumber });
    }
    render() {
        <div className="pure-u-1-1">
            {this.state.pageNumber === 0 ? <p></p> : null}
            {this.state.pageNumber === 1 ? <p></p> : null}
            {this.state.pageNumber === 2 ? <p></p> : null}
            {this.state.pageNumber === 3 ? <p></p> : null}
        </div>
    }
}

The better way is to add onClickLink function to the link of pages, and put all your pages in an array, and in the place where you want it to be have something like this: 更好的方法是将onClickLink函数添加到页面链接,并将所有页面放入数组中,并放置在您希望具有以下内容的位置:

    onClickLink = (pageNumber, event) => {
            this.setState({ pageNumber });
        }

    render() {
            <ul className="pure-menu-list">
                <li className="pure-menu-item"><a href="#" className="pure-menu-link" onClick=onClickLink.bind(null, 2)>Page 2</a></li>
             </ul>


            <div className="pure-u-1-1">
                <p>arrayOfPageComponents[this.state.pageNumber]</p>
            </div>
        }

So the function is bound to the link with the value of page number (in my example 2), when it's called, it's called with 2, so state will be changed to 2, this change will be detected by React and the component will be re-rendered, now reading 2nd value from the array of pages. 因此,该函数以页码的值绑定到链接(在我的示例2中),当调用它时,它被调用为2,因此状态将更改为2,React将检测到此更改,并且组件将被重新渲染,现在从页面数组中读取第二个值。

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

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