单击按钮时的条件格式 JSX
Conditionally Formatting JSX on Button Click
我有一个应用首先显示基于道具的汽车列表,但单击 sort
按钮会将显示转换为按字母顺序排列的列表。
我正在尝试设置一个函数,每次单击 sort
按钮时都会更改 buttonClicked
的状态(运行s sortAlphabetically
。
React 文档中的条件渲染教程侧重于按钮 中 的文本,但我试图根据是否有条件地在我的渲染方法中渲染 JSX 或没有单击单独的按钮。
到目前为止,这是我的代码,returns Parsing error: this is a reserved word
。
import React, { Component } from 'react';
import { connect } from 'react-redux';
import CarCard from '../components/CarCard';
import CarForm from './CarForm';
import './Cars.css';
import { getCars } from '../actions/cars';
import { sortCar } from '../actions/cars';
Component.defaultProps = {
cars: { cars: [] }
}
class Cars extends Component {
constructor(props) {
super(props)
this.state = {
cars: [],
sortedCars: [],
buttonClicked: false
};
}
sortAlphabetically = () => {
handleSortClick();
const newArray = [].concat(this.props.cars.cars)
const orgArray = newArray.sort(function (a,b) {
var nameA = a.name.toUpperCase();
var nameB = b.name.toUpperCase();
if (nameA < nameB) {
return -1;
} else if (nameA > nameB) {
return 1;
}
return 0;
})
this.setState({ cars: {cars: orgArray} })
}
componentDidMount() {
this.props.getCars()
// this.setState({cars: this.props.cars})
}
handleSortClick() {
this.setState({
buttonClicked: !this.state.buttonClicked})
}
render() {
const buttonClicked = this.state.buttonClicked
let display;
if (this.state.buttonClicked = false) {
display = {this.state.cars.cars && this.state.cars.cars.map(car => <CarCard key={car.id} car={car}/>)}
} else {
{this.props.cars.cars && this.props.cars.cars.map(car => <CarCard key={car.id} car={car} />)}
}
return (
<div className="CarsContainer">
<h3>Cars Container</h3>
<button onClick={this.sortAlphabetically}>Sort</button>
{display}
<CarForm />
</div>
);
}
}
const mapStateToProps = (state) => {
return ({
cars: state.cars
})
}
const mapDispatchToProps = (dispatch) => {
return {
sortCar: (cars) => dispatch(sortCar(cars)),
getCars: (cars) => dispatch(getCars(cars))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Cars);
最终,我正在尝试执行以下操作:
- 将
buttonClicked
的初始状态设置为 false
- 每当
sortAplhabetically
为 运行 时切换 buttonClicked
的布尔值
- 当false
时显示this.state.cars.cars && this.state.cars.cars.map(car => <CarCard key={car.id} car={car}/>)}
- 当 true
时显示 {this.props.cars.cars && this.props.cars.cars.map(car => <CarCard key={car.id} car={car} />)}
非常感谢任何帮助。
您可以使用三元运算符。像这样
{this.state.buttonClicked ? ({this.props.cars.cars && this.props.cars.cars.map(car => <CarCard key={car.id} car={car} />)}) : (this.state.cars.cars && this.state.cars.cars.map(car => <CarCard key={car.id} car={car}/>)})}
这是我能想到的最简单的解决方案。您将需要在某些地方替换您自己的代码。但是您的某些代码已被删除,因为它会导致问题并使问题过于复杂。您应该能够在此答案中找到解决问题的方法,但是它不会像复制粘贴那样简单。
您的清单:
- 将 buttonClicked 的初始状态设置为 false:完成
- 每当 sortAplhabetically 为 运行 时切换 buttonClicked 的布尔值:我没有这样做,而是简单地对汽车数组进行排序。
对于其他两个清单项目(由于代码格式问题而省略)。你可能想使用 state,而不是 if X state otherwise props。
class Cars extends Component {
constructor (props) {
super(props)
this.state = {
carList: [{ id: 'apple' }, { id: 'orange' }, { id: 'zealot' }]
}
}
sortAlphabetical () {
this.setState({
carList: this.state.carList.reverse()
})
}
render () {
const CarsList = () => {
return this.state.carList.map(car => {
return <li>{car.id}</li>
})
}
return (
<div className='CarsContainer'>
<h3>Cars Container</h3>
<button onClick={this.sortAlphabetical.bind(this)}>Sort</button>
<CarsList />
</div>
)
}
}
注意:我省略了您的一些代码,因为我无法在不访问所有使用的文件的情况下重新创建您的环境。这应该足以理解基本思想。
将我的 JSX 存储在将根据条件呈现的变量中:
render() {
let message
if (this.state.buttonClicked) {
message = this.state.cars.cars && this.state.cars.cars.map(car => <CarCard key={car.id} car={car}/>)
} else {
message = this.props.cars.cars && this.props.cars.cars.map(car => <CarCard key={car.id} car={car} />)
}
return (
<div className="CarsContainer">
<h3>Cars Container</h3>
<button onClick={this.sortAlphabetically}>Sort</button>
{message}
<CarForm />
</div>
);
}
}
我有一个应用首先显示基于道具的汽车列表,但单击 sort
按钮会将显示转换为按字母顺序排列的列表。
我正在尝试设置一个函数,每次单击 sort
按钮时都会更改 buttonClicked
的状态(运行s sortAlphabetically
。
React 文档中的条件渲染教程侧重于按钮 中 的文本,但我试图根据是否有条件地在我的渲染方法中渲染 JSX 或没有单击单独的按钮。
到目前为止,这是我的代码,returns Parsing error: this is a reserved word
。
import React, { Component } from 'react';
import { connect } from 'react-redux';
import CarCard from '../components/CarCard';
import CarForm from './CarForm';
import './Cars.css';
import { getCars } from '../actions/cars';
import { sortCar } from '../actions/cars';
Component.defaultProps = {
cars: { cars: [] }
}
class Cars extends Component {
constructor(props) {
super(props)
this.state = {
cars: [],
sortedCars: [],
buttonClicked: false
};
}
sortAlphabetically = () => {
handleSortClick();
const newArray = [].concat(this.props.cars.cars)
const orgArray = newArray.sort(function (a,b) {
var nameA = a.name.toUpperCase();
var nameB = b.name.toUpperCase();
if (nameA < nameB) {
return -1;
} else if (nameA > nameB) {
return 1;
}
return 0;
})
this.setState({ cars: {cars: orgArray} })
}
componentDidMount() {
this.props.getCars()
// this.setState({cars: this.props.cars})
}
handleSortClick() {
this.setState({
buttonClicked: !this.state.buttonClicked})
}
render() {
const buttonClicked = this.state.buttonClicked
let display;
if (this.state.buttonClicked = false) {
display = {this.state.cars.cars && this.state.cars.cars.map(car => <CarCard key={car.id} car={car}/>)}
} else {
{this.props.cars.cars && this.props.cars.cars.map(car => <CarCard key={car.id} car={car} />)}
}
return (
<div className="CarsContainer">
<h3>Cars Container</h3>
<button onClick={this.sortAlphabetically}>Sort</button>
{display}
<CarForm />
</div>
);
}
}
const mapStateToProps = (state) => {
return ({
cars: state.cars
})
}
const mapDispatchToProps = (dispatch) => {
return {
sortCar: (cars) => dispatch(sortCar(cars)),
getCars: (cars) => dispatch(getCars(cars))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Cars);
最终,我正在尝试执行以下操作:
- 将
buttonClicked
的初始状态设置为 false - 每当
sortAplhabetically
为 运行 时切换 - 当false 时显示
- 当 true 时显示
buttonClicked
的布尔值
this.state.cars.cars && this.state.cars.cars.map(car => <CarCard key={car.id} car={car}/>)}
{this.props.cars.cars && this.props.cars.cars.map(car => <CarCard key={car.id} car={car} />)}
非常感谢任何帮助。
您可以使用三元运算符。像这样
{this.state.buttonClicked ? ({this.props.cars.cars && this.props.cars.cars.map(car => <CarCard key={car.id} car={car} />)}) : (this.state.cars.cars && this.state.cars.cars.map(car => <CarCard key={car.id} car={car}/>)})}
这是我能想到的最简单的解决方案。您将需要在某些地方替换您自己的代码。但是您的某些代码已被删除,因为它会导致问题并使问题过于复杂。您应该能够在此答案中找到解决问题的方法,但是它不会像复制粘贴那样简单。
您的清单:
- 将 buttonClicked 的初始状态设置为 false:完成
- 每当 sortAplhabetically 为 运行 时切换 buttonClicked 的布尔值:我没有这样做,而是简单地对汽车数组进行排序。
对于其他两个清单项目(由于代码格式问题而省略)。你可能想使用 state,而不是 if X state otherwise props。
class Cars extends Component {
constructor (props) {
super(props)
this.state = {
carList: [{ id: 'apple' }, { id: 'orange' }, { id: 'zealot' }]
}
}
sortAlphabetical () {
this.setState({
carList: this.state.carList.reverse()
})
}
render () {
const CarsList = () => {
return this.state.carList.map(car => {
return <li>{car.id}</li>
})
}
return (
<div className='CarsContainer'>
<h3>Cars Container</h3>
<button onClick={this.sortAlphabetical.bind(this)}>Sort</button>
<CarsList />
</div>
)
}
}
注意:我省略了您的一些代码,因为我无法在不访问所有使用的文件的情况下重新创建您的环境。这应该足以理解基本思想。
将我的 JSX 存储在将根据条件呈现的变量中:
render() {
let message
if (this.state.buttonClicked) {
message = this.state.cars.cars && this.state.cars.cars.map(car => <CarCard key={car.id} car={car}/>)
} else {
message = this.props.cars.cars && this.props.cars.cars.map(car => <CarCard key={car.id} car={car} />)
}
return (
<div className="CarsContainer">
<h3>Cars Container</h3>
<button onClick={this.sortAlphabetically}>Sort</button>
{message}
<CarForm />
</div>
);
}
}