How to toggle on Order in ReactJS

一个人想着一个人 提交于 2020-12-11 05:02:45

问题


I am doing sorting in asc and desc. I want to make logic if User click first time I want to update it with name asc, when User click second time I want to update it with name desc. It will repeat same manner when ever user click.

Code

        class Example extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      Item: 5,
      skip: 0
    }

    this.handleClick = this.handleClick.bind(this);
  }

  urlParams() {
    return `http://localhost:3001/meetups?filter[limit]=${(this.state.Item)}&&filter[skip]=${this.state.skip}`
  }

  handleClick() {
    this.setState({skip: this.state.skip + 1})
  }

  render() {
    return (
      <div>
        <a href={this.urlParams()}>Example link</a>
        <pre>{this.urlParams()}</pre>
        <button onClick={this.handleClick}>Change link</button>
      </div>
    )
  }
}


ReactDOM.render(<Example/>, document.querySelector('div#my-example' ))

回答1:


You can toggle based on what you have in the state.

getSortedData = () => {
  this.setState({
    sortedData: this.state.sortedData === "name asc" ? "name desc" : "name asc"
  }, () => {
    this.getData();
  });
};

So this one will work on the following way:

sortedData = "name asc";
console.log(sortedData);
setInterval(function () {
  sortedData = sortedData === "name asc" ? "name desc" : "name asc";
  console.log(sortedData);
}, 500);



回答2:


This is a good question. I think you can do something like that.

class App extends React.Component {
  state = {
    names: ['Ane', 'Robert', 'Jane'],
    asc: true,
  };
  
  toggleOrder = () => {
    this.setState({
      names: this.state.asc
        ? this.state.names.sort()
        : this.state.names.reverse(),
      asc: !this.state.asc,
    });
  }
  
  render() {
    const { names, asc } = this.state;
    
    return (
      <div>
        <button
          onClick={this.toggleOrder}
        >
          {
            asc ? 'des' : 'asc'
          }
        </button>
        <ul>
          {
            names.map(name => (
              <li>{name}</li>
            ))      
          }
        </ul>
      </div>
    );
  }
}


ReactDOM.render(
  <App />,
  document.querySelector('#app')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>



回答3:


define order in your component state and initialize it with true.

constructor(props){
        super(props);
        this.state={
          order: true, 
          sortedData: ''
        }
       }
       
        getSortedData =() => {
            
            if (this.state.order) {
              this.setState({order: false, sortedData: 'name desc' }, () => {
                this.getData();
              });
            } else {
              this.setState({order: true, sortedData: 'name asc' }, () => {
                this.getData();
              });
            }
        }


来源:https://stackoverflow.com/questions/55283338/how-to-toggle-on-order-in-reactjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!