Add a conditional in the reactjs rendering - reactjs

I have a component that returns me a data that I want to display.
However I would like to add a conditional that would allow me to display my table only if it contains data.
We must use the if and else conditional for the display of the table.
Is there another solution to not display the map when it is empty?
import React from 'react';
import {Card, Col, Row } from 'react-bootstrap';
function Int(props: any) {
let calendarPlanned: any[] = props.calendarPlannedData;
return (
<Card>
<Card.Body>
{calendarPlanned.map((planned) => (
<Col xs={12} lg={12} className="no-gutters" key={planned.id}>
<Row>
<Col
xs={3}
lg={3}
className="no-gutters"
>
{planned.plannedDate}
</Col>
<Col
xs={2}
lg={2}
className="d-flex"
>
</Col>
<Col
xs={7}
lg={7}
className="d-flex"
>
<strong
className="d-block text-truncate"
>
{planned.stepName}
</strong>
</Col>
</Row>
</Col>
))}
</Card.Body>
</Card>
);
}
export default Int;

Use conditional rendering by checking array.length > 0 to render the table as follows.
import React from "react";
import { Card, Col, Row } from "react-bootstrap";
function Int(props: any) {
let calendarPlanned: any[] = props.calendarPlannedData;
return (
<Card>
<Card.Body>
{calendarPlanned.length > 0 &&
calendarPlanned.map((planned) => (
<Col xs={12} lg={12} className="no-gutters" key={planned.id}>
<Row>
<Col xs={3} lg={3} className="no-gutters">
{planned.plannedDate}
</Col>
<Col xs={2} lg={2} className="d-flex"></Col>
<Col xs={7} lg={7} className="d-flex">
<strong className="d-block text-truncate">
{planned.stepName}
</strong>
</Col>
</Row>
</Col>
))}
</Card.Body>
</Card>
);
}
export default Int;

Check if data exists and if exists it it's length is greater than 0 then render your stuff else just render nothing
{calenderPlanned && calendarPlanned.length > 0 ? calendarPlanned.map((planned) => (....) : null}

You could use length property or just check if calenderPlanned has at least one element:
{calendarPlanned.length > 0 && calendarPlanned.map((planned) => (....))}
Or
{calendarPlanned[0] && calendarPlanned.map((planned) => (....))}
Of course this is valid if and even if calendarPlanned is already defined. If props.calendarPlannedData could be undefined, you should modify your condition in this way:
{calendarPlanned && calendarPlanned.length > 0 && calendarPlanned.map((planned) => (....))}
Or
{calendarPlanned && calendarPlanned[0] && calendarPlanned.map((planned) => (....))}

Related

How do I pass one string of an array each time a component is rendered?

This component renders four cards for top teams in a database, they are already sorted by highest scores; I need a <p> tag that displays either "1st", "2nd", "3rd", "4th" on each one. The current code displays "1st2nd3rd4th" in the <p> tag of each card.
const seed = ["1st", "2nd", "3rd", "4th"];
const place = seed.map((seed) => seed);
const TeamCard = ({data}) => {
return (
<TeamCardStyle>
{!data && (
<Spinner />
)}
{data && data.getGroupScores && (data.getGroupScores.slice(0, 4).map((group) => (
<Row>
<Col className="teamCard mt-2 mb-2">
<Row>
<p># {place}</p>
</Row>
<Row>
<Col className="hideSmall">
<img className="mouseOn" src="../images/group.png" />
<img className="mouseOff" src="../images/groupSelected.png" />
</Col>
</Row>
<p>{group.name}</p>
</Col>
</Row>
))
)}
</TeamCardStyle>
)
}
export default TeamCard;
Happy Hacking!
You can use index from map function to get the relevant element from seed array.
const seed = ["1st", "2nd", "3rd", "4th"];
const TeamCard = ({data}) => {
return (
<TeamCardStyle>
{!data && (
<Spinner />
)}
{data && data.getGroupScores && (
data.getGroupScores.slice(0, 4).map((group, index) => {
return (
<Row>
<Col className="teamCard mt-2 mb-2">
<Row>
<p># {seed[index]}</p>
</Row>
<Row>
<Col className="hideSmall">
<img className="mouseOn" src="../images/group.png" />
<img className="mouseOff" src="../images/groupSelected.png" />
</Col>
</Row>
<p>{group.name}</p>
</Col>
</Row>
)
})
)}
</TeamCardStyle>
)
}
export default TeamCard

How to do onChange with React Numeric Input

I'm trying to use a spinbox in my code but whenever I try changing the values using onChange, the app crashes.
I also tried using onValueChange but it's getting ignored and I don't know what the problem is:
item.js
import NumericInput from "react-numeric-input";
import { Row, Col, ListGroup } from "react-bootstrap";
function ItemScreen() {
const [qty, setQty] = useState(1);
return (
<div>
<h2 className="text-center mb-3">Order</h2>
<hr></hr>
<Row>
<Col md={8}>
<ListGroup variant="flush">
<ListGroup.Item>
<Row>
<Col>Apple</Col>
<Col>
<NumericInput
min={1}
max={100}
onChange={(e) => setQty(e.target.value)}
value={qty}
/>
</Col>
<Col>
500 x {qty} = {qty * 500}
</Col>
</Row>
</ListGroup.Item>
</ListGroup>
</Col>
</Row>
</div>
);
}
export default ItemScreen;
the NumericInput component event just return the numeric value so you can this I guess :
<NumericInput
min={1}
max={100}
onChange={(value) => setQty(value)}
/>
The value will always be of type string.
So you need to cast it as a number.
onChange={(e) => setQty(Number(e.target.value))}

Increment or decrement a value in react array while mapping

export default class Cart extends Component {
constructor(props) {
super(props);
this.state = {
selectedForCart: [],
checkOut: true,
priceQuantity: undefined,
total: undefined,
};
}
render() {
return (
<div>
<NavBar />
{this.state.selectedForCart &&
this.state.selectedForCart.map((v, i) => {
return (
<Container className="mt-5" key={i}>
<Row>
<Col lg={2} md={2} sm={12}>
<p>
Price: ₹
{v.priceTaxIncluded}
</p>
<p style={{ fontSize: "10px" }}>Tax inclusive</p>
</Col>
<Col lg={2} md={2} sm={12}>
<Form.Group>
<Form.Label>Quantity</Form.Label>
<Form.Control
type="number"
placeholder="Quantity"
value={1}
onChange={(e) => {
this.setState({
priceQuantity: e.target.value
});
console.log(v.priceTaxIncluded * e.target.value);
}}
/>
</Form.Group>
</Col>
<Col lg={2} md={2} sm={12}>
{this.state.checkOut && (
<>
<p>{this.state.priceQuantity}</p>
</>
)}
</Col>
</Row>
</Container>
);
})}
<Footer />
</div>
);
}
}
Here in the above code, I'm mapping an array of items from the state object "" value. And if I increment a specific item, then that specific item's quantity only should increment. But it is not happening all items are getting incremented. And also the price should be multiplied with the incremented value and should be shown...
Thank You.
As in this image, I'm changing the quantity. But If I change the quantity of a product, then its reflecting the price for all products. As I have only one state object
You can bind the priceQuantity to input value. Then conditionally render in last column. As you are trying to print priceQuantity in last column it will show for all rows.
this.state = {
...
priceQuantity: 0,
..
};
{this.state.selectedForCart &&
this.state.selectedForCart.map((v, i) => {
return (
<Container className="mt-5" key={i}>
<Row>
<Col lg={2} md={2} sm={12}>
<p>
Price: ₹
{v.priceTaxIncluded}
</p>
<p style={{ fontSize: "10px" }}>Tax inclusive</p>
</Col>
<Col lg={2} md={2} sm={12}>
<Form.Group>
<Form.Label>Quantity</Form.Label>
<Form.Control
type="number"
placeholder="Quantity"
value={this.state.priceQuantity}
onChange={(e) => {
this.setState({
priceQuantity: e.target.value
});
console.log(v.priceTaxIncluded * e.target.value);
}}
/>
</Form.Group>
</Col>
<Col lg={2} md={2} sm={12}>
{this.state.checkOut && (
<>
<p>{this.state.priceQuantity ? v.priceTaxIncluded * this.state.priceQuantity : 0}</p>
</>
)}
</Col>
</Row>
</Container>
);
})}

react slick slider convert class component to functional component

i am using react slick...and using class component, now i just want to convert class component to functional component....because in these component i need to use another function component...it getting some problem....could any one help on this thing...below the class i have used (renderArrows =) this i thing is getting problem...
this is the link of code sand box : https://codesandbox.io/s/jovial-smoke-ndzhj
import React, {Component } from 'react';
import { Col, Row } from 'react-grid-system';
import { ButtonBase } from '#material-ui/core';
import ArrowBackIosIcon from '#material-ui/icons/ArrowBackIos';
import ArrowForwardIosIcon from '#material-ui/icons/ArrowForwardIos';
import Slider from "react-slick";
class Example extends Component {
renderArrows = () => {
return (
<div className="slider-arrow">
<ButtonBase
className="arrow-btn prev"
onClick={() => this.slider.slickPrev()}
>
<ArrowBackIosIcon />
</ButtonBase>
<ButtonBase
className="arrow-btn next"
onClick={() => this.slider.slickNext()}
>
<ArrowForwardIosIcon />
</ButtonBase>
</div>
);
};
render() {
return (
<div>
<Col xl={12} lg={12} md={12} sm={12} xs={12} className="desktop-slider">
<div className="education-level main-boxes boxex-bottom">
<Row className="row">
<Col xl={4} lg={4} md={4} sm={12} xs={12}>
<div className="index-box-head horizontal-box-head">
1. Education Level
</div>
</Col>
<Col xl={8} lg={8} md={8} sm={12} xs={12}>
<div style={{ position: "relative" }}>
{this.renderArrows()}
<Slider
ref={c => (this.slider = c)}
dots={false}
arrows={false}
centerMode={true}
slidesToShow={1}
infinite={false}>
<li>Home
</li>
<li>About Us
</li>
<li>Gallery
</li>
</Slider>
</div>
</Col>
</Row>
</div>
</Col>
</div>
);
}
}
export default Example;
This can be the best answer
I tried and made this work with you sample
Check please
https://codesandbox.io/s/infallible-turing-wgvrb?file=/src/App.js
import React, { useRef } from "react";
import { Col, Row } from "react-grid-system";
import { ButtonBase } from "#material-ui/core";
import ArrowBackIosIcon from "#material-ui/icons/ArrowBackIos";
import ArrowForwardIosIcon from "#material-ui/icons/ArrowForwardIos";
import Slider from "react-slick";
const Example = () => {
const customSlider = useRef();
const renderArrows = () => {
return (
<div className="slider-arrow">
<ButtonBase
className="arrow-btn prev"
onClick={() => customSlider.current.slickPrev()}
>
<ArrowBackIosIcon />
</ButtonBase>
<ButtonBase
className="arrow-btn next"
onClick={() => customSlider.current.slickNext()}
>
<ArrowForwardIosIcon />
</ButtonBase>
</div>
);
};
return (
<div>
<Col xl={12} lg={12} md={12} sm={12} xs={12} className="desktop-slider">
<div className="education-level main-boxes boxex-bottom">
<Row className="row">
<Col xl={4} lg={4} md={4} sm={12} xs={12}>
<div className="index-box-head horizontal-box-head">
1. Education Level
</div>
</Col>
<Col xl={8} lg={8} md={8} sm={12} xs={12}>
<div style={{ position: "relative" }}>
{renderArrows()}
<Slider
ref={slider => (customSlider.current = slider)}
dots={false}
arrows={false}
centerMode={true}
slidesToShow={1}
infinite={false}
>
<li>Home</li>
<li>About Us</li>
<li>Gallery</li>
</Slider>
</div>
</Col>
</Row>
</div>
</Col>
</div>
);
};
export default Example;
class component to functional component:
import React, { Component } from 'react'
import { Col, Row } from 'react-grid-system'
import { ButtonBase } from '#material-ui/core'
import ArrowBackIosIcon from '#material-ui/icons/ArrowBackIos'
import ArrowForwardIosIcon from '#material-ui/icons/ArrowForwardIos'
import Slider from 'react-slick'
const renderArrows = () => {
return (
<div className="slider-arrow">
<ButtonBase
className="arrow-btn prev"
onClick={() => this.slider.slickPrev()}
>
<ArrowBackIosIcon />
</ButtonBase>
<ButtonBase
className="arrow-btn next"
onClick={() => this.slider.slickNext()}
>
<ArrowForwardIosIcon />
</ButtonBase>
</div>
)
}
const Example = () => {
return (
<div>
<Col xl={12} lg={12} md={12} sm={12} xs={12} className="desktop-slider">
<div className="education-level main-boxes boxex-bottom">
<Row className="row">
<Col xl={4} lg={4} md={4} sm={12} xs={12}>
<div className="index-box-head horizontal-box-head">
1. Education Level
</div>
</Col>
<Col xl={8} lg={8} md={8} sm={12} xs={12}>
<div style={{ position: 'relative' }}>
{renderArrows()}
<Slider
ref={c => (this.slider = c)}
dots={false}
arrows={false}
centerMode={true}
slidesToShow={1}
infinite={false}
>
<li>Home</li>
<li>About Us</li>
<li>Gallery</li>
</Slider>
</div>
</Col>
</Row>
</div>
</Col>
</div>
)
}
export default Example
One more thing, I don't seem to get from where this.slider.slickNext() came from. I think it would be this.Slider.slickPrev().
If I'm right, then for functional component, change it to Slider.slickPrev()

Couldn't Import a File From Multiple Layers Folders in My React Context

I am practicing my React Context. I created Provider and trying to use the Consumer from multiple layers of folders. However I'm having trouble importing my CountContext. It kept giving me this error message
./src/Count/Test1Folder/Test2Folder/Test3Folder/Test4Folder/TestValue.js
Module not found: Can't resolve './Count/CountContext' in
'/Users/qiangbarejie/Documents/web_dev_training/react/mycontext/src/Count/Test1Folder/Test2Folder/Test3Folder/Test4Folder'"
Here is my code, I'd appreciate if anybody could help me out, thanks!
I created a React.createContex separate folder named Count and the file called CountContext.js with directory path:
src/Count/CountContext.js
import React from 'react'
export const CountContext = React.createContext();
My App.js look like this below:
import React, {Component} from 'react'
import {CountContext} from './Count/CountContext'
import Container1 from './Count/Container1'
import Container2 from './Count/Container2'
import {Grid, Col, Row} from 'react-bootstrap'
import './App.css';
import NavBar from './Nav/NavBar'
import Products1 from './Pages/Products1'
import Products2 from './Pages/Products2'
import Products3 from './Pages/Products3'
import Products4 from './Pages/Products4'
import Products5 from './Pages/Products5'
import Products6 from './Pages/Products6'
import Products7 from './Pages/Products7'
import Products8 from './Pages/Products8'
import TestValue from './Count/Test1Folder/Test2Folder/Test3Folder/Test4Folder/TestValue'
class App extends Component{
constructor(props){
super(props);
this.state={
text: '',
contextState:{
count: 0,
increment: this.increment,
clearValue: this.clearValue,
test: this.testValue
}
};
}
testValue = () => {
this.setState({
contextState: { test: this.state.contextState.test = "TEST"}
})
}
increment = () => {
this.setState({
contextState: {...this.state.contextState, count: this.state.contextState.count + 1} });
}
clearValue = () => {
this.setState({
contextState: {...this.state.contextState, count: this.state.contextState.count = 0}
})
}
onChange = e => {
const {value, name} = e.target;
this.setState({[name]: value});
}
render(){
return(
<CountContext.Provider value={this.state.contextState}>
<Grid className="gridwrapper">
<React.Fragment>
<Row className="show-grid">
<Col>
<NavBar />
</Col>
<Col xs={6} md={3}>
<div>
#1 Col xs={6} md={3}
<br />
Count: {this.state.contextState.count}</div>
<Products1 />
<TestValue />
</Col>
<Col xs={6} md={3}>
#2 Col xs={6} md={3}
<Products2 />
</Col>
<Col xs={6} md={3}>
#2 Col xs={6} md={3}
<Products2 />
</Col>
<Col xs={6} md={3}>
#2 Col xs={6} md={3}
<Products2 />
</Col>
</Row>
<Row className="show-grid">
<Col xs={6} md={3}>
#3 Col xs={6} md={3}
<Container1 />
<input name="text" value={this.state.text} onChange={this.onChange} />
<Products3 />
</Col>
<Col xs={6} md={3}>
#4 Col xs={6} md={3}
<Container2 />
<Products4 />
</Col>
<Col xsHidden md={3}>
<div>
#5 Col xsHidden md={3}
<Products5 />
</div>
</Col>
<Col xsHidden md={3}>
<div>
#5 Col xsHidden md={3}
<Products5 />
</div>
</Col>
</Row>
<hr />
<Row className="show-grid">
<Col xs={6} xsOffset={3}>
<div>
#6 Col xs={6} xsOffset={3}
<Products6 />
</div>
</Col>
<Col xs={6} xsOffset={3}>
<div>
#6 Col xs={6} xsOffset={3}
<Products6 />
</div>
</Col>
</Row>
<hr />
<Row className="show-grid">
<Col md={6} mdPush={6}>
<div>
#7 Col md={6} mdPush={6}
<Products7 />
</div>
</Col>
<Col md={6} mdPull={6}>
<div>
#8 Col md={6} mdPull={6}
<Products8 />
</div>
</Col>
</Row>
</React.Fragment>
</Grid>
</CountContext.Provider>
)
}
}
export default App
And I created a file named TestValue.js under this directory path
src/Count/Test1Folder/Test2Folder/Test3Folder/Test4Folder/TestValue.js
import React, {Component} from 'react'
import {CountContext} from './Count/CountContext'
class TestValue extends Component {
render(){
return(
<CountContext.Consumer>
{testValue => testValue = {testValue}}
</CountContext.Consumer>
)
}
}
export default TestValue

Resources