如何在 ReactJS 中开启订单
How to toggle on Order in ReactJS
我正在 asc
和 desc
中进行排序。如果用户第一次点击我想用 name asc
更新它,当用户第二次点击我想用 name desc
更新它时,我想制定逻辑。每当用户点击时,它将以相同的方式重复。
代码
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' ))
您可以根据您所在州的情况进行切换。
getSortedData = () => {
this.setState({
sortedData: this.state.sortedData === "name asc" ? "name desc" : "name asc"
}, () => {
this.getData();
});
};
所以这个将按以下方式工作:
sortedData = "name asc";
console.log(sortedData);
setInterval(function () {
sortedData = sortedData === "name asc" ? "name desc" : "name asc";
console.log(sortedData);
}, 500);
这是个好问题。我认为你可以做类似的事情。
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>
在您的组件状态中定义 order
并使用 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();
});
}
}
我正在 asc
和 desc
中进行排序。如果用户第一次点击我想用 name asc
更新它,当用户第二次点击我想用 name desc
更新它时,我想制定逻辑。每当用户点击时,它将以相同的方式重复。
代码
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' ))
您可以根据您所在州的情况进行切换。
getSortedData = () => {
this.setState({
sortedData: this.state.sortedData === "name asc" ? "name desc" : "name asc"
}, () => {
this.getData();
});
};
所以这个将按以下方式工作:
sortedData = "name asc";
console.log(sortedData);
setInterval(function () {
sortedData = sortedData === "name asc" ? "name desc" : "name asc";
console.log(sortedData);
}, 500);
这是个好问题。我认为你可以做类似的事情。
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>
在您的组件状态中定义 order
并使用 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();
});
}
}