Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I have an array with data, where I want to show 3 entries as a default. 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. 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

Can someone help me out?

like image 549
ST80 Avatar asked Jun 05 '17 13:06

ST80


People also ask

How do you add Show More button in react JS?

const [showMore, setShowMore] = useState(false); 3. Create condition and the button where you want to show text as follow: In this case, it means if showMore is true, it will show the text.

How do you toggle multiple buttons in React?

Onclick function execute the myChangeHandler , which changes the state to opposite on every click. This will toggle the content inside h1 element. Here the function execute the change for both button.


1 Answers

Here is one solution. JSBin here

First, put all of your component data into its 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. We can use an expanded variable to change the button text based on the user's action. We're also binding showMore to this component so that the button knows what component's method to call when clicked.

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. Then we can change the button's text depending on the expanded value like so

<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() {
  this.state.itemsToShow === 3 ? (
    this.setState({ itemsToShow: this.state.cars.length, expanded: true })
  ) : (
    this.setState({ itemsToShow: 3, expanded: false })
  )
}
like image 162
glhrmv Avatar answered Oct 14 '22 12:10

glhrmv