简体   繁体   English

ReactJS如何添加show more / show less按钮

[英]ReactJS How to add a show more/show less button

I'm new to React and I want to add a simple "Show more" button to my Application. 我是React的新手,我想在我的应用程序中添加一个简单的“显示更多”按钮。 I have an array with data, where I want to show 3 entries as a default. 我有一个数据数组,我想在默认情况下显示3个条目。 When a user clicks on show more , the rest of the data should be rendered, and the Button should change the text to show less . 当用户点击show more ,应该呈现其余数据,Button应该更改文本以show less I'm not exactly sure how to do it. 我不确定该怎么做。

This is what I got so far: 这是我到目前为止所得到的:

class Application extends React.Component {
  constructor() {
    super()

    this.cars = [
     { "name" : "Audi", "country" : "Germany"},
     { "name" : "BMW", "country" : "Germany" },
     { "name" : "Chevrolet", "country" : "USA" },
     { "name" : "Citroen", "country" : "France" },
     { "name" : "Hyundai", "country" : "South Korea" },
     { "name" : "Mercedes-Benz", "country" : "Germany" },
     { "name" : "Renault", "country" : "France" },
     { "name" : "Seat", "country" : "Spain" },
   ]

   this.isLoaded = false

  }

  showMore() {
   // show more entries
   // switch to "show less"
  }

  render() {
    return <div className="container">
     <h3>Click show more to see more data</h3>
     <div className="row">
       <h3>List of Cars</h3>
       <ul>
         {this.cars.slice(0,3).map((car, i) => <li key={i}>{car.name} - {car.country}</li>)}
       </ul>
     </div>
     <p>
      <a className="btn btn-primary" onClick={this.showMore()}>Show more</a>.
     </p>
   </div>;
  }
}

React.render(<Application />, document.getElementById('app'));

Here is a JSBin with the code in action 这是一个带有代码的JSBin

Can someone help me out? 有人可以帮我吗?

Here is one solution. 这是一个解决方案。 JSBin here JSBin在这里

First, put all of your component data into its state . 首先,将所有组件数据置于其state

this.state = {
  cars: [
    { "name" : "Audi", "country" : "Germany"},
    { "name" : "BMW", "country" : "Germany" },
    { "name" : "Chevrolet", "country" : "USA" },
    { "name" : "Citroen", "country" : "France" },
    { "name" : "Hyundai", "country" : "South Korea" },
    { "name" : "Mercedes-Benz", "country" : "Germany" },
    { "name" : "Renault", "country" : "France" },
    { "name" : "Seat", "country" : "Spain" },
  ],
  itemsToShow: 3,
  expanded: false
}

this.showMore = this.showMore.bind(this);

Let's use itemsToShow to know how many items to show when not expanded. 让我们使用itemsToShow来了解未展开时要显示的项目数。 We can use an expanded variable to change the button text based on the user's action. 我们可以使用expanded变量根据用户的操作更改按钮文本。 We're also binding showMore to this component so that the button knows what component's method to call when clicked. 我们还将showMore绑定到此组件,以便按钮知道单击时要调用的组件的方法。

In our render, let's change a few things, like so 在我们的渲染中,让我们改变一些事情,就像这样

<ul>
  {this.state.cars.slice(0, this.state.itemsToShow).map((car, i) => 
    <li key={i}>{car.name} - {car.country}</li>
  )}
</ul>

We're going to render from 0 to however many itemsToShow we have. 我们将从0渲染到很多itemsToShow我们有。 Then we can change the button's text depending on the expanded value like so 然后我们可以根据expanded值更改按钮的文本

<a className="btn btn-primary" onClick={this.showMore}>
  {this.state.expanded ? (
    <span>Show less</span>
  ) : (
    <span>Show more</span>
  )}
</a>.

Finally, let's write the showMore method that actually changes the value of itemsToShow . 最后,让我们写的showMore实际值改变方法itemsToShow

showMore() {
  this.state.itemsToShow === 3 ? (
    this.setState({ itemsToShow: this.state.cars.length, expanded: true })
  ) : (
    this.setState({ itemsToShow: 3, expanded: false })
  )
}

You do some things wrong. 你做错了什么。 You need to understand what a state is and how react interact with it. 您需要了解状态是什么以及反应如何与之相互作用。 When a state change, your render function is called again, it allow you to have dynamic rendering based on your current state. 当状态改变时,再次调用渲染函数,它允许您根据当前状态进行动态渲染。

First, initialize your state with what you want (here, I could have put only rowsToDisplay, but I guess you want to be able to update your cars as well). 首先,用你想要的东西初始化你的状态(在这里,我可以只放置rowsToDisplay,但我想你也希望能够更新你的汽车)。

You should bind this to your function showMore so when it's called, it get that "this" refers to your Component. 你应该将它绑定到你的函数showMore,所以当它被调用时,它会得到“this”指的是你的Component。

OnClick, showMore will be called; OnClick,showMore将被调用; the function update your state. 该功能更新您的状态。 By updating it, render will be called again (remember that a change in your state call render again with your new values). 通过更新它,将再次调用渲染(请记住,状态调用的更改将再次使用新值进行渲染)。 This should be the only way for you to update the view. 这应该是您更新视图的唯一方法。

class Application extends React.Component {
  constructor(props) {
    super(props)

    this.state = {cars : [
      { "name" : "Audi", "country" : "Germany"},
      { "name" : "BMW", "country" : "Germany" },
      { "name" : "Chevrolet", "country" : "USA" },
      { "name" : "Citroen", "country" : "France" },
      { "name" : "Hyundai", "country" : "South Korea" },
      { "name" : "Mercedes-Benz", "country" : "Germany" },
      { "name" : "Renault", "country" : "France" },
      { "name" : "Seat", "country" : "Spain" },
    ],    
    rowsToDisplay : 4};
    this.showMore = this.showMore.bind(this);
  }

  showMore() {
    let carLength = this.state.cars.length;
    this.setState({rowsToDisplay:carLength});
    // show more entries
    // switch to "show less"
  }
  render() {
    return <div className="container">
      <h3>Click show more to see more data</h3>
      <div className="row">
        <h3>List of Cars</h3>
        <ul>
          {this.state.cars.slice(0,this.state.rowsToDisplay).map((car, i) => <li key={i}>{car.name} - {car.country}</li>)}
        </ul>
      </div>
      <p>
        <a className="btn btn-primary" onClick={this.showMore}>Show more</a>.
      </p>
    </div>;
  }
}

React.render(<Application />, document.getElementById('app'));

Feel free to ask questions, but please read the doc first : https://facebook.github.io/react/docs/state-and-lifecycle.html 随意提问,但请先阅读文档: https//facebook.github.io/react/docs/state-and-lifecycle.html

 class Application extends React.Component { constructor() { super() this.state = { loadMore: false } this.cars = [ { "name" : "Audi", "country" : "Germany"}, { "name" : "BMW", "country" : "Germany" }, { "name" : "Chevrolet", "country" : "USA" }, { "name" : "Citroen", "country" : "France" }, { "name" : "Hyundai", "country" : "South Korea" }, { "name" : "Mercedes-Benz", "country" : "Germany" }, { "name" : "Renault", "country" : "France" }, { "name" : "Seat", "country" : "Spain" }, ] this.isLoaded = false } showMore() { // show more entries // switch to "show less" this.setState({loadMore: true}) } render() { const loadCount = this.state.loadMore ? this.cars.length : 3; return ( <div className="container"> <h3>Click show more to see more data</h3> <div className="row"> <h3>List of Cars</h3> <ul> {this.cars.slice(0,loadCount).map((car, i) => <li key={i}>{car.name} - {car.country}</li>)} </ul> </div> <p> <a> className="btn btn-primary" onClick={this.showMore()}>Show more</a> </p> </div> ); } } React.render(<Application />, document.getElementById('app')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app" /> 
use this.setState() to reRender the component! 使用this.setState()来reRender组件!

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

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