react slick slider convert class component to functional component - reactjs

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()

Related

Add a conditional in the reactjs rendering

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) => (....))}

React.JS Object passing during onclick event

I am new to react,
I am using card, when card "on click" i need to get the card object and place it in form dynamically,
(full code)
My code:
// reactstrap components
import {useState} from "react"
import {
Card,
CardHeader,
CardBody,
CardTitle,
Col,
Input,
FormGroup,
Form,
Container,
Row,
UncontrolledDropdown,
DropdownToggle,
DropdownItem,
DropdownMenu,
Button,
} from "reactstrap";
import Header from "components/Headers/Header.js";
import 'react-toastify/dist/ReactToastify.css';
import Navbar from "components/Navbars/modulenavbar"
import axios from "axios";
import React from "react";
import { Link } from "react-router-dom";
var apitoken= localStorage.getItem('apitoken');
const api=axios.create({baseURL:"https://api/v1/account"})
const options = {
headers: {'Authorization': apitoken}
}
const Accounts = () => {
const [accounts, setaccount] = React.useState([]);
const [loading, setLoading] = React.useState(true);
const [disabled, setDisabled] = useState(false);
React.useEffect(async () => {
const response = await api.get("/",options);
setaccount(response.data.response);
setLoading(false);
}, []);
if (loading) {
return <>Loading...</>;
}
function handleGameClick() {
setDisabled(!disabled);
}
This is were i get all my api value and append it
return (
<>
{accounts.map((student, index) => {
const { id, name, phone, email } = student //destructuring
return (
<>
<div style={{ width: "18rem" }} onClick={() => console.log(student)}>
I want to pass the object "Student" and use it in the default value of the forms shown below
<Card className="card-stats mb-4 mb-lg-1">
<CardBody>
<Row>
<div className="col">
<CardTitle className="h4 font-weight-bold mb-0">
{name}
</CardTitle>
<span className="h5">{phone}</span>
</div>
<div className="col">
<span className="h5">{email}</span>
</div>
</Row>
</CardBody>
</Card>
</div>
</>
)
})}
</>
)
}
Form Shows here
const Display = () => {
return (
<>
<Header />
<Container className="mt--7" fluid>
{/* Table */}
<Row>
<Col className="order-xl-1 " xl="2">
<CardHeader className="bg-white border-0">
<Row className="align-items-center">
<Col xs="8">
<Link to="/admin//accounts" className="ni ni-bold-left">
<span> View Account</span></Link>
</Col>
</Row>
</CardHeader>
<Card className="bg-secondary shadow navbar-nav-scroll">
<Accounts/>
</Card>
</Col>
<Col className="order-xl-1" xl="10">
<Card className="bg-secondary shadow">
<Navbar/>
<Row >
<Col className="shadow navbar-nav-scroll">
<Form>
<h6 className="heading-small text-muted mb-4">
Account Information
</h6>
<div className="pl-lg-4">
<Row>
<Col >
<FormGroup>
<label
className="form-control-label"
htmlFor="input-username"
>
Account Name
</label>
<Input
className="form-control-alternative"
id="input-username"
placeholder="Username"
type="text"
defaultValue={student.value}
/>
</FormGroup>
</Col>
</Row>
<Row>
<Col >
<FormGroup>
<label
className="form-control-label"
htmlFor="input-email"
>
Email address
</label>
<Input
className="form-control-alternative"
id="input-email"
placeholder="jesse#example.com"
type="email"
/>
</FormGroup>
</Col>
</Row>
<Row>
<Col >
<FormGroup>
<label
className="form-control-label"
htmlFor="input-email"
>
Phone
</label>
<Input
className="form-control-alternative"
id="input-phone"
placeholder="Phone"
type="text"
/>
</FormGroup>
</Col>
</Row>
</div>
</Form>
</Col>
<Col xs="9">
<Card className="card-stats mb-4 mb-lg-0">
<CardBody>
<div>
<Row className="align-items-center">
<Col xs="8">
</Col>
<Col className="text-right" xs="4">
<Button
color="success"
href="#pablo"
// onClick={save}
>
Save
</Button>
</Col>
</Row>
</div>
</CardBody>
</Card>
</Col>
</Row>
</Card>
</Col>
</Row>
</Container>
</>
);
};
export default Display;
Note:The above three sections of code is in the single component
I just want to dynamically append the values from object during "on click" event
Thanks in advance
You can store the clicked student value in state and pass it on to whichever component needs it as props
const Accounts = () => {
const [selectedStudent, setSelectedStudent] = useState({});
...
const handleStudentClick = (student) => {
setSelectedStudent(student)
}
return (
<>
{accounts.map((student, index) => {
const { id, name, phone, email } = student //destructuring
return (
<>
<div style={{ width: "18rem" }} onClick={() => handleStudentClick(student)}>
Now you can pass selected student as props to your child component

React : Unable to set the values in the form

I want to set the values of the form if the edit is true. I am passing the editable state to true and passing the first element of the array but when the component is mounted it didn't show the 0th element values that were sent from the parent component to the child component.
Here's the code :
Milestone.js
import React, { Component } from 'react';
import "./milestone.css";
import MileStoneForm from './mileStoneForm'
import {LEFT_ARROW, ROUTES_PATH } from "../../constants";
export default class MilestoneComp extends Component {
constructor(props) {
super(props);
this.state = {
arr: [],
edit:true
}
}
backPage = () => {
this.props.history.push(ROUTES_PATH.CREATE_PROJECT)
}
handleData=(data)=>{
const {arr}=this.state
arr.push(data)
//localStorage.setItem('mileStoneData',JSON.stringify(arr))
console.log(arr)
}
render() {
return (
<div style={{ background: "#F3F3F3", minHeight: "100vh" }}>
<div className="header-div">
<img
src={LEFT_ARROW}
alt="back"
style={{ cursor: "pointer" }}
className="left-arrow-size left-back-btn-bt-mar"
onClick={this.backPage}
/>
</div>
<MileStoneForm arr={this.state.arr[0]} handleData={this.handleData} edit={this.state.edit}/>
</div>
)
}
}
MileStoneForm.js
import { InputBase,Grid,TextareaAutosize} from '#material-ui/core'
import React, { Component } from 'react'
import { DROP_D } from "../../constants";
import { PRIVATE_SwITCH,PUBLIC_SwITCH } from "../../constants";
import NormalButton from '../../common/component/normalButton';
import DatePicker from 'react-datepicker'
import 'react-datepicker/dist/react-datepicker.css';
class MileStoneForm extends Component {
constructor(props){
super(props)
this.state={
deliverable_name:"",
due_date:"",
deliverable_notes:"",
milestone_based_payment:false,
deliverable_name_error:"",
due_date_error:"",
deliverable_notes_error:"",
percent_rate:0,
percent_rate_error:""
}
}
componentDidMount(){
console.log('component did mount')
if(this.props.edit===true){
console.log('editable')
if(this.props.arr){
console.log('array')
this.setState({
deliverable_name:this.props.arr.milestoneName,
deliverable_notes:this.props.arr.description,
due_date:this.props.arr.dueDate,
milestone_based_payment:this.props.arr.isMilestoneBasedPayment,
percent_rate:this.props.arr.percentageRate
},()=>{
console.log('edit')
})
}
}
}
render() {
const {deliverable_name,deliverable_name_error,deliverable_notes,deliverable_notes_error,
due_date,due_date_error,milestone_based_payment,}=this.state
return (
<>
<div className="milestone">
<div className="milestone-header">ADD MILESTONE</div>
<Grid container className="milestone-deliverable-name-date">
<Grid item md={6} lg={6} xs={12}>
<div className="milestone-deliverable-name">DELIVERABLE NAME</div>
<InputBase
className={`milestone-input-deliverable-name`}
autoComplete={"off"}
placeholder={"MileStone Name"}
onChange={e=>this.handleChange(e,'deliverable_name')}
value={deliverable_name}
maxLength="100"
autoFocus={true}/>
{deliverable_name_error && (
<div className="input-error-style">{deliverable_name_error}</div>
)}
</Grid>
<Grid item md={6} lg={6} xs={12}>
<div className="milestone-due-date">
DUE DATE
</div>
<label>
<DatePicker
dateFormat="MM/dd/yyyy"
margin="normal"
selected={due_date}
placeholderText="Due Date"
onChange={date=>this.handleChangeDate(date,'due_date')}
maxDate={new Date()}
className={`milestone-input-due-date`}
/>
<img src={DROP_D} alt="drop down" style={{cursor:'pointer'}} className='dropdown-milestone'/>
</label>
{due_date_error && (
<div className="input-error-style">{due_date_error}</div>
)}
</Grid>
<Grid item md={12} lg={12} xs={12}>
<div className="milestone-notes-description">
<div className="milestone-deliverable-notes">DELIVERABLE NOTES</div>
<div className="milestone-description-notes">Add description below</div>
<TextareaAutosize className={`milestone-textarea-description`}
onChange={(e)=>this.handleChange(e,'deliverable_notes')}
value={deliverable_notes}/>
{deliverable_notes_error && (
<div className="input-error-style">{deliverable_notes_error}</div>
)}
</div>
</Grid>
<Grid item md={12} lg={12} xs={12}>
<div className="milestone-payment">MILESTONE BASED PAYMENT?</div>
{this.togglePayment()}
</Grid>
<Grid item md={12} lg={12} xs={12}>
{milestone_based_payment ?<>
<div className="percent-rate">PERCENT RATE</div>
<InputBase
className={`milestone-percent-rate`}
autoComplete={"off"}
placeholder={"20%"}
maxLength="100"
value={this.state.percent_rate}
onChange={(e)=>{this.handleChange(e, "percent_rate")}}
/>
{
this.state.percent_rate_error && (
<div className="input-error-style">{this.state.percent_rate_error}</div>
)
}
</> :''}
</Grid>
<Grid item xs={12} sm={12} md={4} lg={4}></Grid>
<Grid item xs={12} sm={12} md={4} lg={4}></Grid>
<Grid item xs={12} sm={12} md={4} lg={4}>
<div className={milestone_based_payment?"milestone-button":"milestone-button-margin-btm"}>
<NormalButton
buttonValue="ADD"
className="btn-create-project flex-justify"
icon_color="black"
handleButtonAction={()=>this.handleSubmit()}
/>
</div>
</Grid>
</Grid>
</div>
</>
)
}
}
export default MileStoneForm
You are mutating your state with push, never do that. You have to create a new state object.
handleData=(data)=>{
const { arr }= this.state
const nextArr= {...arr, data}
//localStorage.setItem('mileStoneData',JSON.stringify(arr))
this.setState({arr: nextArr});
console.log(arr)
}
The console.log will show the new data anyway, since you are changing that object, but since the object reference will be the same (same object as before), it will not rerender.

How to fix Creative Tim material design dashboard charts incomplete display with material UI animations?(Grow, Slide etc_

I'm having the following problem as shown in picture. The Component of creative tim's charts width and height is messed up. This is because of using Grow Transition of material UI. This is when first time component is loaded with transition
First Image
When i re-click on same tab, It loads perfectly fine but there's no transition since component is already mounted as shown in the following picture
second image
I've tried to use fixed width and height for the charts but no avail.
So how do i make work of this chart perfectly with material UI transitions(Grow,Slide) without loading problems?
PS: I'm designing my own Interface with animation and transitions based on creative tim theme and material UI. It's using multiple transitions on same page during load.
Code:
import React from "react";
// react plugin for creating charts
import ChartistGraph from "react-chartist";
// #material-ui/core
import { makeStyles } from "#material-ui/core/styles";
import Icon from "#material-ui/core/Icon";
// #material-ui/icons
import Store from "#material-ui/icons/Store";
import Warning from "#material-ui/icons/Warning";
import DateRange from "#material-ui/icons/DateRange";
import LocalOffer from "#material-ui/icons/LocalOffer";
import Update from "#material-ui/icons/Update";
import ArrowUpward from "#material-ui/icons/ArrowUpward";
import AccessTime from "#material-ui/icons/AccessTime";
import Accessibility from "#material-ui/icons/Accessibility";
import BugReport from "#material-ui/icons/BugReport";
import Code from "#material-ui/icons/Code";
import Cloud from "#material-ui/icons/Cloud";
// core components
import GridItem from "components/Grid/GridItem.js";
import GridContainer from "components/Grid/GridContainer.js";
import Table from "components/Table/Table.js";
import Tasks from "components/Tasks/Tasks.js";
import CustomTabs from "components/CustomTabs/CustomTabs.js";
import Danger from "components/Typography/Danger.js";
import Card from "components/Card/Card.js";
import CardHeader from "components/Card/CardHeader.js";
import CardIcon from "components/Card/CardIcon.js";
import CardBody from "components/Card/CardBody.js";
import CardFooter from "components/Card/CardFooter.js";
import { bugs, website, server } from "variables/general.js";
import {
dailySalesChart,
emailsSubscriptionChart,
completedTasksChart,
} from "variables/charts.js";
import { Grow } from "#material-ui/core";
import styles from "assets/jss/material-dashboard-react/views/dashboardStyle.js";
const useStyles = makeStyles(styles);
export default function ProductsHome(props) {
const classes = useStyles();
return (
<div>
<Grow in={true} timeout={1000}>
<div>
<GridContainer>
<GridItem xs={12} sm={6} md={3}>
<Card>
<CardHeader color="warning" stats icon>
<CardIcon color="warning">
<Icon>content_copy</Icon>
</CardIcon>
<p className={classes.cardCategory}>Used Space</p>
<h3 className={classes.cardTitle}>
49/50 <small>GB</small>
</h3>
</CardHeader>
<CardFooter stats>
<div className={classes.stats}>
<Danger>
<Warning />
</Danger>
<a href="#pablo" onClick={(e) => e.preventDefault()}>
Get more space
</a>
</div>
</CardFooter>
</Card>
</GridItem>
<GridItem xs={12} sm={6} md={3}>
<Card>
<CardHeader color="success" stats icon>
<CardIcon color="success">
<Store />
</CardIcon>
<p className={classes.cardCategory}>Revenue</p>
<h3 className={classes.cardTitle}>$34,245</h3>
</CardHeader>
<CardFooter stats>
<div className={classes.stats}>
<DateRange />
Last 24 Hours
</div>
</CardFooter>
</Card>
</GridItem>
<GridItem xs={12} sm={6} md={3}>
<Card>
<CardHeader color="danger" stats icon>
<CardIcon color="danger">
<Icon>info_outline</Icon>
</CardIcon>
<p className={classes.cardCategory}>Fixed Issues</p>
<h3 className={classes.cardTitle}>75</h3>
</CardHeader>
<CardFooter stats>
<div className={classes.stats}>
<LocalOffer />
Tracked from Github
</div>
</CardFooter>
</Card>
</GridItem>
<GridItem xs={12} sm={6} md={3}>
<Card>
<CardHeader color="info" stats icon>
<CardIcon color="info">
<Accessibility />
</CardIcon>
<p className={classes.cardCategory}>Followers</p>
<h3 className={classes.cardTitle}>+245</h3>
</CardHeader>
<CardFooter stats>
<div className={classes.stats}>
<Update />
Just Updated
</div>
</CardFooter>
</Card>
</GridItem>
</GridContainer>
<GridContainer>
<GridItem xs={12} sm={12} md={4}>
<Card chart>
<CardHeader color="success">
<ChartistGraph
className="ct-chart"
data={dailySalesChart.data}
type="Line"
options={dailySalesChart.options}
listener={dailySalesChart.animation}
/>
</CardHeader>
<CardBody>
<h4 className={classes.cardTitle}>Daily Sales</h4>
<p className={classes.cardCategory}>
<span className={classes.successText}>
<ArrowUpward
className={classes.upArrowCardCategory}
/>{" "}
55%
</span>{" "}
increase in today sales.
</p>
</CardBody>
<CardFooter chart>
<div className={classes.stats}>
<AccessTime /> updated 4 minutes ago
</div>
</CardFooter>
</Card>
</GridItem>
<GridItem xs={12} sm={12} md={4}>
<Card chart>
<CardHeader color="warning">
<ChartistGraph
className="ct-chart"
data={emailsSubscriptionChart.data}
type="Bar"
options={emailsSubscriptionChart.options}
responsiveOptions={
emailsSubscriptionChart.responsiveOptions
}
listener={emailsSubscriptionChart.animation}
/>
</CardHeader>
<CardBody>
<h4 className={classes.cardTitle}>Email Subscriptions</h4>
<p className={classes.cardCategory}>
Last Campaign Performance
</p>
</CardBody>
<CardFooter chart>
<div className={classes.stats}>
<AccessTime /> campaign sent 2 days ago
</div>
</CardFooter>
</Card>
</GridItem>
<GridItem xs={12} sm={12} md={4}>
<Card chart>
<CardHeader color="danger">
<ChartistGraph
className="ct-chart"
data={completedTasksChart.data}
type="Line"
options={completedTasksChart.options}
listener={completedTasksChart.animation}
/>
</CardHeader>
<CardBody>
<h4 className={classes.cardTitle}>Completed Tasks</h4>
<p className={classes.cardCategory}>
Last Campaign Performance
</p>
</CardBody>
<CardFooter chart>
<div className={classes.stats}>
<AccessTime /> campaign sent 2 days ago
</div>
</CardFooter>
</Card>
</GridItem>
</GridContainer>
<GridContainer>
<GridItem xs={12} sm={12} md={6}>
<CustomTabs
title="Tasks:"
headerColor="primary"
tabs={[
{
tabName: "Bugs",
tabIcon: BugReport,
tabContent: (
<Tasks
checkedIndexes={[0, 3]}
tasksIndexes={[0, 1, 2, 3]}
tasks={bugs}
/>
),
},
{
tabName: "Website",
tabIcon: Code,
tabContent: (
<Tasks
checkedIndexes={[0]}
tasksIndexes={[0, 1]}
tasks={website}
/>
),
},
{
tabName: "Server",
tabIcon: Cloud,
tabContent: (
<Tasks
checkedIndexes={[1]}
tasksIndexes={[0, 1, 2]}
tasks={server}
/>
),
},
]}
/>
</GridItem>
<GridItem xs={12} sm={12} md={6}>
<Card>
<CardHeader color="warning">
<h4 className={classes.cardTitleWhite}>
Employees Stats
</h4>
<p className={classes.cardCategoryWhite}>
New employees on 15th September, 2016
</p>
</CardHeader>
<CardBody>
<Table
tableHeaderColor="warning"
tableHead={["ID", "Name", "Salary", "Country"]}
tableData={[
["1", "Dakota Rice", "$36,738", "Niger"],
["2", "Minerva Hooper", "$23,789", "CuraƧao"],
["3", "Sage Rodriguez", "$56,142", "Netherlands"],
["4", "Philip Chaney", "$38,735", "Korea, South"],
]}
/>
</CardBody>
</Card>
</GridItem>
</GridContainer>
</div>
</Grow>
</div>
);
}
So after struggling a while, I moved from React-chartlist to react-chartjs-2 which is wrapper of chartjs for charts being displayed

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