I am trying to render a child component only if a condition is met but no matter what I try, nothing really works. I get all sort of errors.
My application is a store than contains a list of fruits available to clients. I want to render only the items selected into their basket. That's what Facture.js is trying to do. I'm trying to check the count of every fruits in the list that has count > 0
and nothing when it's 0.
Right now, it's rendering everything in the list, even though I do not want them – I know in this actual code there is no condition, so it returns everything.
Shouldn't it just be as simple as an if block?
Here is my full code structure if it helps :
App.js
import Client from "./Client";
state = { fruits : [{name: "Apple", price: "3", count: 2},
{name: "Orange", price: "2", count: 0}] }
render() { <Client fruits={this.state.fruits}/> }
// I tried this : {this.props.fruits.count > 0 ? <Factures fruits={this.props.fruits} /> : null} but it returns nothing.
Client.js
import Factures from "./Factures";
render() { <Factures fruits={this.props.fruits}/> }
Factures.js
import Facture from "./Facture";
render() {
return this.props.fruits.map(fruit => <Facture key={fruit.nom} fruit={fruit} /> );
}
Facture.js
class Facture extends React.Component {
render() {
return (
<li className='list-group-item d-flex justify-content-between small'>
<div className=''>{this.props.fruit.name}</div>
<div className=''>{'x' + this.props.fruit.count}</div>
</li>
);
}
}
export default Facture;