How to display data from JSON as flex row in div ReactJS - reactjs

Here is my code. I want to display data from JSON file as flex-row in div. But what I got is column. I use tailwind to display it as flex-row. Here is the expected output: https://i.stack.imgur.com/f8uAp.jpg
import React, { Component } from "react";
import PostData from "../data/data.json";
class Sneaker extends Component {
state = {};
render() {
return (
<div>
{PostData.map((postDetail) => {
return (
<div className="flex flex-row bg-gray-400 w-2/3 mx-auto mt-10">
<div className="text-center w-1/4 p-2 m-2 bg-white">
<img className="w-64 h-64" src={postDetail.image} alt="" />
<h1>{postDetail.name}</h1>
<h1 className="text-red-500">${postDetail.price}</h1>
</div>
</div>
);
})}
</div>
);
}
}
export default Sneaker;

Try to put the postData loop inside the flex-row div as below
<div className="flex flex-row bg-gray-400 w-2/3 mx-auto mt-10">
{PostData.map((postDetail) => {
return (
<div className="text-center w-1/4 p-2 m-2 bg-white">
<img className="w-64 h-64" src={postDetail.image} alt="" />
<h1>{postDetail.name}</h1>
<h1 className="text-red-500">${postDetail.price}</h1>
</div>
);
})}
</div>

If you want put 3 items in a flex row you can write code like this:
import React, { Component } from "react";
import PostData from "../data/data.json";
const perRow = 3;
class Sneaker extends Component {
state = {};
render() {
return (
<div>
{PostData.map((ignoreIt, i) => {
return i % perRow === 0 ? (
<div
className="flex flex-row bg-gray-400 w-2/3 mx-auto mt-10"
key={i}
>
{PostData.slice(i, i + perRow).map((postDetail, j) => {
return (
<div
className="text-center w-1/4 p-2 m-2 bg-white"
key={i + j}
>
<img className="w-64 h-64" src={postDetail.image} alt="" />
<h1>{postDetail.name}</h1>
<h1>{j}</h1>
<h1 className="text-red-500">${postDetail.price}</h1>
</div>
);
})}
</div>
) : null;
})}
</div>
);
}
}
export default Sneaker;

Related

How to update card name when clicking on card in React JS

I am building this app in React Js where i can search for a specific card and once i click on that card the card info section gets updated with the card name, description and image. I have 1 parent component called Decks that contains 2 child components called Search and within Search i have another component called CardsFiltered. The issue i am facing is passing data from child component to its parent component up the tree. How can i tackle this issue? Any idea's will be appreciated.
Decks.js
import React, { useEffect, useState } from 'react'
import { DragDropContext } from 'react-beautiful-dnd'
import CardsSearch from './CardsSearch'
import Search from './Search'
function Decks(props) {
const image = require('../images/blue-eyes-white-dragon.png')
const [name, setName] = useState()
useEffect(() => {
window.addEventListener('storage', (e) => {
const localName = localStorage.getItem('items')
console.log(localName)
this.setName(localName)
})
}, [])
return (
<div className='h-screen w-full flex flex-row'>
<div className='flex flex-row h-full w-full'>
<div className='flex flex-col h-full w-[55rem] gap-6 p-4'>
<div className='flex border-solid border-2 border-black p-2'>
<h1 onChange={(e) => setName(e.target.value)}>{name}</h1>
</div>
<div className='flex justify-center w-[16rem]'>
<img src={image} loading="lazy" />
</div>
<div className='flex flex-row border-black border-solid border-2 p-2'>
<p>Card Desc</p>
</div>
</div>
<div className='flex flex-col w-[100rem] gap-3 p-4'>
<div className='flex flex-row justify-between border-black border-solid border-2 p-2'>
<div>
<p>Main [40]</p>
</div>
<div className='flex flex-row gap-3'>
<p>NM[6]</p>
<p>EM[17]</p>
<p>SP[13]</p>
<p>TR[4]</p>
</div>
</div>
<div className='flex flex-row flex-wrap border-black border-2 border-solid'>
<DragDropContext>
<img src={image} alt="Card" className='w-20' />
<img src={image} alt="Card" className='w-20' />
<img src={image} alt="Card" className='w-20' />
<img src={image} alt="Card" className='w-20' />
<img src={image} alt="Card" className='w-20' />
</DragDropContext>
</div>
<div className='flex flex-row justify-between border-black border-solid border-2 p-2'>
<div>
<p>Extra [11]</p>
</div>
<div className='flex flex-row gap-3'>
<p>FM[6]</p>
<p>SM[6]</p>
<p>XYZ[2]</p>
<p>RM[1]</p>
</div>
</div>
<div className='flex flex-row flex-wrap border-black border-2 border-solid'>
<img src={image} alt="Card" className='w-20' />
<img src={image} alt="Card" className='w-20' />
<img src={image} alt="Card" className='w-20' />
<img src={image} alt="Card" className='w-20' />
<img src={image} alt="Card" className='w-20' />
</div>
<div className='flex flex-row justify-between border-black border-solid border-2 p-2'>
<div>
<p>Side [13]</p>
</div>
<div className='flex flex-row gap-3'>
<p>NM[6]</p>
<p>EM[17]</p>
<p>SP[13]</p>
<p>TR[4]</p>
</div>
</div>
<div className='flex flex-row flex-wrap border-black border-2 border-solid'>
<img src={image} alt="Card" className='w-20' />
<img src={image} alt="Card" className='w-20' />
<img src={image} alt="Card" className='w-20' />
</div>
</div>
<div className='flex flex-col w-[45rem] overflow-y-auto'>
<div className='flex flex-col'>
<Search getName={name => setName(name)} />
</div>
</div>
</div>
</div>
)
}
export default Decks
Search.js
import React, { useState, Suspense } from 'react'
import { useRef } from 'react'
//import CardsFiltered from './CardsFiltered'
const CardsFiltered = React.lazy(() => import('./CardsFiltered'))
function Search(props) {
const [inputText, setInputText] = useState("")
let inputHandler = (e) => {
var lowerCase = e.target.value.toLowerCase()
setInputText(lowerCase)
}
const clickPoint = useRef();
const handleFocus = () => {
clickPoint.current.style.display = "none";
};
const handleBlur = () => {
clickPoint.current.style.display = "block";
};
const [cardName, setCardName] = useState()
return (
<>
<div className="items-center py-6 px-4 flex justify-center" >
<div className="relative mr-3">
<div className="absolute top-3 left-3 items-center" ref={clickPoint}>
<svg className="w-5 h-5 text-gray-500" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fillRule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clipRule="evenodd"></path></svg>
</div>
<input
type="text"
className="block p-2 pl-10 w-70 text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:pl-3"
placeholder="Search Here..."
onFocus={handleFocus}
onBlur={handleBlur}
onChange={inputHandler}
/>
</div>
</div>
<div>
<Suspense fallback={<div>Loading...</div>}>
<CardsFiltered input={inputText} onMouseOver={() => props.getName(cardName)} getCardName={cardName => setCardName(cardName)}/>
</Suspense>
</div>
</>
)
}
export default Search
CardsFiltered.js
import React, { useEffect, useState } from 'react'
import Cards from '../../JsonData/FormattedData.json'
import { AiFillStar } from 'react-icons/ai';
function CardsFiltered(props) {
const filteredData = Cards.data.filter((e) => {
if (props.input === '') {
return e
}
else {
//console.log(e)
return e.name.toLowerCase().includes(props.input)
}
})
let lastIndex = 50
const [name, setName] = useState()
useEffect(() => {
localStorage.setItem('items', JSON.stringify(name))
//console.log(name)
}, [name])
return (
<>
<div className='flex justify-center align-middle px-4 overflow-y-auto overflow-x-hidden font-normal'>
<div className='box-border h-auto w-[35rem] border-2 border-slate-600 flex flex-col justify-center align-middle'>
{filteredData.splice(0, lastIndex).map((card) => {
return (
<div key={card.id} onMouseOver={() => setName(card.name)} className='flex h-full w-full justify-center align-middle box-border border-2 gap-3 overflow-auto myCard'>
<div className='flex h-40 w-56 justify-center align-middle'>
<img src={card.card_images[0].image_url_small} className='w-auto h-auto' alt="Yugioh Card Image" loading='lazy'
/>
</div>
<div className='flex flex-col justify-center align-middle w-full'>
<h4><b>{card.name}</b></h4>
<p>{card.attribute}/{card.race}</p>
<span><AiFillStar/> {card.level}</span>
<p>{card.atk}/{card.def}</p>
</div>
</div>
)
})}
</div>
</div>
</>
)
}
export default CardsFiltered
I understand this can be done with the state hook and/or with props but so far no luck using props.

How change LabelFormat value at y axis in syncfusion charts?

I make a chart with React and syncfusion but I dont'know what is the best way to change the y axis values. I solve so I search the id and mannualy write the DOM in useEffect.
But what is the best way I change y axis values in syncfusion charts?
import React, { useEffect } from "react";
import { skillsIcon } from "../data/dummy";
import { GoPrimitiveDot } from "react-icons/go";
import { Stacked, Pie, Button, SparkLine,BackendStacked } from "../components";
import {
earningData,
SparklineAreaData,
ecomPieChartData,
} from "../data/dummy";
import { useStateContext } from "../contexts/ContextProvider";
const Skills = () => {
const {
screenSize,
setScreenSize,
} = useStateContext();
useEffect(() => {
document.getElementById("charts1_AxisLabel_4").innerHTML = "Medior"
document.getElementById("charts1_AxisLabel_3").innerHTML = ""
document.getElementById("charts1_AxisLabel_2").innerHTML = "Junior"
document.getElementById("charts1_AxisLabel_1").innerHTML = ""
document.getElementById("charts1_AxisLabel_0").innerHTML = "Entry"
}, [screenSize,setScreenSize]);
return (
<div>
<h1 className="text-center text-4xl my-10 font-bold">Skills</h1>
<div className="flex m-3 flex-wrap justify-center gap-1 items-center">
{skillsIcon.map((skill) => (
<div
key={skill.title}
className="bg-white dark:text-gray-200 dark:bg-secondary-dark-bg md:w-40
p-4 pt-9 rounded-2xl"
>
<button
type="button"
style={{ color: skill.iconColor, backgroundColor: skill.iconBg }}
className="text-2xl opacity-0.9 rounded-full p-4 hover:drop-shadow-xl"
>
{skill.icon}
</button>
<p className="mt-3">
<span className="text-lg font-semibold">{skill.title}</span>
<span className={`text-sm text-${skill.pcColor} ml-2`}>
{skill.percentage}
</span>
</p>
</div>
))}
</div>
<div className="flex gap-10 flex-wrap justify-center">
<div
className="bg-white dark:text-gray-200
dark:bg-secondary-dark-bg m-3 p-4 rounded-2xl md:w-780"
>
<div className="">
<h2 className="font-semibold text-center text-xl ">
Skills - graph
</h2>
</div>
<div className="mt-10 flex gap-10 flex-wrap justify-center">
<div>
<Stacked height="420px" width="580px" />
</div>
<div>
<BackendStacked height="420px" width="580px" />
</div>
</div>
</div>
</div>
</div>
);
};
export default Skills;
This requirement can be achieved using the chart axisLabelRender event. Please review the code snippet and sample link shared below, which changes axis labels using an event.
export let ylabels = ["Zero", "Twenty", "Fourty", "Sixty","Eighty", "Hundred"];
export let i = 0;
<ChartComponent id='charts' axisLabelRender={this.onAxisLabelRender.bind(this)}>
</ChartComponent>
onAxisLabelRender(args){
if (args.axis.name == "primaryYAxis") {
if(ylabels.length == i)
i =0;
args.text = ylabels[i]+" %";
i++;
}
}
Sample : https://stackblitz.com/edit/react-w6o5wf
Here’a some more relevant information from our Documentation to learn more about the axisLabelRender event.
https://ej2.syncfusion.com/react/documentation/chart/axis-labels/#customizing-specific-point
Please let us know if you have any more questions.

The react-icons are not loading in my React/TailwindCSS navbar

I'm using TailwindCSS and react-icons, but I can't seem to load the icons used to toggle my menu.
import { useState } from "react";
import { HiMenuAlt4 } from "react-icons/hi";
import { AiOutlineClose } from "react-icons/ai";
import logo from "../../images/logo.png";
const NavbarItem = ({ title, classProps }) => {
return (
<li className={`mx-4 cursor-pointer ${classProps}`}>
{title}
</li>
);
};
const Navbar = () => {
const [toggleMenu, setToggleMenu] = useState(0);
return (
<nav className="w-full flex md:justify-center justify-between items-center p-4">
<div className="md:flex-[0.5] flex-initial justify-center items-center"
<img src={logo} alt="logo" className="w-32 cursor-pointer" />
</div>
<ul className="text-white md:flex hidden list-none justify-between items-center flex-initial">
{["Market", "Exchange", "Tutorials", "Wallets"].map((item, index) => (
<NavbarItem key={item + index} title={item} />
))}
<li className="bg-[#2952e3] py-2 px-7 mx-4 rounded-full cursor-pointer hover:bg-[#2546bd]">
Login
</li>
</ul>
<div className="flex relative">
{toggleMenu ? (
<AiOutlineClose
fontSize={28}
className="text-white md:hidden cursor-pointer"
onClick={() => setToggleMenu(false)}
/>
) : (
<HiMenuAlt4
fontSize={28}
className="text-white md:hidden cursor-pointer"
onClick={() => setToggleMenu(true)}
/>
)}
</div>
</nav>
);
};
export default Navbar;

button only clicks once, at one div element

this is the onclick function
export function changeColorButton() {
document.getElementById("friendDiv").style.background = "grey";
}
this is the output file. I want every button to be clickable and give background grey
{data.projects.map((project, key) => {
return (
<div id="friendDiv" className="relative">
<div key={key} className="flex flex-row items-center space-x-6 mb-6">
<img src={project.image} />
<div>
<h1 key={key} class=" text-xl font-bold">
{project.name}
</h1>
</div>
<button
className="absolute right-10 bg-bgButtonAddPeople p-2"
onClick={changeColorButton}
>
Legg til
</button>
</div>
</div>
);
});
}
You would just need to componentize the div element that is being mapped. So you can follow an approach like this.
Each FriendDiv element would have its own instance of changeColorButton, so it would apply the color to its own element.
FriendDiv.js
const FriendDiv = ({ key, project }) => {
const [isGrey, setIsGrey] = useState(false);
const changeColorButton = () => {
setIsGrey(!isGrey);
};
return (
<div
style={{ backgroundColor: isGrey ? 'grey' : 'default-color}}
id="friendDiv"
className="relative"
>
<div key={key} className="flex flex-row items-center space-x-6 mb-6">
<img src={project.image} />
<div>
<h1 key={key} class=" text-xl font-bold">
{project.name}
</h1>
</div>
<button
className="absolute right-10 bg-bgButtonAddPeople p-2"
onClick={changeColorButton}
>
Legg til
</button>
</div>
</div>
);
};
App.js
const App = () => {
return data.projects.map((project, key) => {
return <FriendDiv project={project} key={key} />;
});
};

I am trying to import a different product list from an array named clothingproducts

I have three arrays one with all of the items and the other two with specific items. When I change the array name in context it works but when i create another context file with the same code (contextc) I get this error TypeError: Cannot read property 'products' of undefined
return value.products.map(product => {
import React, { Component } from "react";
import Product from "./Product";
import { ProductConsumer } from "../contextc";
export default class Productlist extends Component {
render() {
return (
<React.Fragment>
<header className="bg py-5 mb0 container-fluid clothing border-bottom border-danger">
<div className="container h-100">
<div className="row h-100 align-items-center">
<div className="col-lg-12">
<h1 className="display-4 text-white mt-5 mb-2 text-center">
Clothing
</h1>
<p className="lead mb-5 text-white-50 text-center">
Our latest Releases.
</p>
</div>
</div>
</div>
</header>
<div className="py-0 ">
<div className="container">
<div className="row">
<ProductConsumer>
{value => {
return value.products.map(product => {
return <Product key={product.id} product={product} />;
});
}}
</ProductConsumer>
</div>
</div>
</div>
</React.Fragment>
);
}
}
this is happening because value is undefined.

Resources