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' ))
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);