I have the following code:
import React, { Component } from 'react';
import {Button} from "@material-ui/core";
import Selector from "./Selector"
class Trigger extends Component {
constructor(props) {
super(props);
this.state = {
clicks: 0
};
}
IncrementItem = () => {
this.setState({
clicks: this.state.clicks + 1
});
}
DecreaseItem = () => {
this.setState({
clicks: this.state.clicks - 1
});
}
render() {
console.log(this.state)
return (
<div>
<Button
onClick={this.IncrementItem}
variant="contained"
color='primary'>
add
</Button>
{this.state.clicks ?
<Button
onClick={this.DecreaseItem}
variant="contained"
color='primary'>
remove
</Button>:
null}
{this.state.clicks ?
<Selector>
</Selector>:
null}
</div>
);
}
}
export default Trigger;
Idea:
1. The first button increases this.state.clicks
2. The second button is shown only if this.state.clicks > 0
3. The second button decreases this.state.clicks
Problem: render the Selector component n times (not only one time like in my code), where n=this.state.clicks.
Note: the selector component is composed by 2 selectors and multiple options, so it's not a string-array. Thanks a lot!
Try this:
{
[...Array(this.state.clicks)].map((v, i) => <Selector key={`selector-${i}`} /> )
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.