I am trying to use matrial ui elements,but my issue is that it is not responsive. I want to show 2
items on medium screen and just 1 on small ones, but it is 3 on all screens. thx
this is the parent component and I prop drill the array's elements to the child component
import * as React from "react";
import Box from "#mui/material/Box";
import ImageList from "#mui/material/ImageList";
import ImageListItem from "#mui/material/ImageListItem";
import ImageListItemBar from "#mui/material/ImageListItemBar";
import InfiniteScroll from "react-infinite-scroll-component";
import Grid from "#mui/material/Grid";
import ProductListItem from "./ProductListItem";
const ProductList = ({ products }) => {
return (
<>
<div className="mr-5 ml-5">
<Box>
<ImageList variant="masonry" cols={3} gap={18}>
{products.map((item) => (
<ProductListItem
item={item}
key={item.id}
/>
))}
</ImageList>
</Box>
</div>
</>
);
};
export default ProductList;
this is the child component for each element of array
import React, { useState, useEffect } from "react";
import Heart from "react-heart";
import { useLocalStorage } from "./storage.js";
import ImageListItem from "#mui/material/ImageListItem";
import ImageListItemBar from "#mui/material/ImageListItemBar";
import ImageList from "#mui/material/ImageList";
const ProductListItem = (props) => {
return (
<>
<ImageListItem>
<img
src={`${props.item.img_src}?w=248&fit=crop&auto=format`}
srcSet={`${props.item.img_src}?w=248&fit=crop&auto=format&dpr=2 2x`}
alt={props.item.title}
loading="lazy"
/>
<div style={{ width: "2rem" }} className="ml-2 mt-2">
<Heart />
</div>
<ImageListItemBar
subtitle={<span>by: aaaaaaaaaaaa</span>}
position="below"
align="right"
/>
</ImageListItem>
</>
);
};
export default ProductListItem;
Related
Can you explain me the problem that I have in my code...I am trying to get grapesjs editor into my code...Can anyone help me out on this one??
import React, {useState, useEffect} from 'react';
import grapesjs from 'grapesjs';
import Basics from "grapesjs-blocks-basic"
import BaseReactComponent from "../../base-react-component";
import ReactComponents from "../../react-components";
import MuiComponents from "../../mui-components/index"
import Listing from '../../Listing';
import {Button, Slider, Snackbar} from "#material-ui/core"
import MuiButton from "mui-button";
const EditStepUI = (props) => {
const [editor, setEditor] = useState(true)
useEffect(() => {
const editor = grapesjs.init({
container: "#gjs",
height: "100%",
storageManager: false,
noticeOnUnload: false,
plugins: [Basics, BaseReactComponent, ReactComponents, MuiComponents]
});
console.log(editor);
}, [])
return (
<div id="editor">
editor.setComponents(`
<div>
<span>
Foo
</span>
<Listing>
<div>
Bar
</div>
<MuiButton variant='contained' color='primary'>
Click Me
</MuiButton>
<Slider />
</Listing>
<Snackbar>
<MuiButton variant='contained' color='secondary'>
Click Me
</MuiButton>
</Snackbar>
<Slider />
</div>
`);
</div>
)
}
export default EditStepUI;
I´m trying to update the Detail component data whenever I select a card component.
I´m using useContext hook but I´m stuck at this point and I don´t know how to do this.
Could anybody please help?.
enter image description here
context.js
import { createContext, useContext, useEffect, useState } from "react";
import api from "./api/players"
const APIcontext = createContext();
export function Provider({children}){
const [players, setPlayers] = useState([]);
const [currentPlayer, setCurrentPlayer] = useState(null)
useEffect(() => {
const fetchPlayers = async () => {
try{
const response = await api.get('/players');
setPlayers(response.data)
}
catch(err){
if(err.response){
console.log(err.response.data)
}else{
console.log(`Error: ${err.message}`)
}
}
}
fetchPlayers()
},[])
const UpdateDetails = () =>{
}
return(
<APIcontext.Provider value={{players, currentPlayer,UpdateDetails}}>{children}</APIcontext.Provider>
)
}
export default APIcontext;
This is the Detail Component where I want to display the data, whenever a card is selected on click.
Details.jsx
import React from "react";
import Button from "react-bootstrap/Button";
import Card from "react-bootstrap/Card";
import {useContext} from "react";
import APIcontext from "../context";
function Details() {
const {players} = useContext(APIcontext)
return (
<Card style={{width:"80%", marginRight:"20px"}}>
<Card.Header className="text-center"><h1>Details</h1>
</Card.Header>
<Card.Body className="px-5">
<h4>{players.realName}</h4>
<h4>{players.realName}</h4>
<h4>{players.assets}</h4>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
);
}
export default Details;
This is the Card component that displays the data from the players. By clicking on one of them the Details component should be updated.
Card Component
[enter code here
import React, { useEffect, useState, useContext } from "react";
import Card from "react-bootstrap/Card";
import APIcontext from "../context";
function Cards({}) {
const { players } = useContext(APIcontext);
console.log("players", players);
const { UpdateDetails} = useContext(APIcontext)
return (
<>
{players &&
players.map((player) => (
<Card
key={player.id}
className="mt-4 mx-2"
style={{
display: "flex",
width: "12rem",
height: "9rem",
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
}}
>
<Card.Body onClick={UpdateDetails}>
<Card.Title>{player.realName}</Card.Title>
<Card.Subtitle className="mb-2 text-muted">
{player.playerName}
</Card.Subtitle>
<Card.Text>{player.asset}</Card.Text>
</Card.Body>
</Card>
))}
</>
);
}
export default Cards;][2]
App.js
import "./App.css";
import "bootstrap/dist/css/bootstrap.min.css";
import Card from "./components/Card";
import Details from "./components/Details";
import { Container, Row, Col } from "react-bootstrap";
import Controls from "./components/Controls";
import { useEffect, useState } from "react";
import { Provider } from "./context";
function App() {
return (
<div className="App">
<Provider>
<Container className="p-3 d-flex flex-row justify-content-between">
<Container className="p-5 d-flex flex-row mb-4 bg-light rounded-3">
<Details />
<Controls />
</Container>
</Container>
<Container className=" mr-5 mb-4 bg-light rounded-3 " style={{width:"65%", float:"left", marginLeft:"10%"}}>
<Container className="d-flex flex-row flex-justify-content-center flex-wrap mr-5 mb-4 bg-light rounded-3" >
<Card className="mr-2"/>
</Container>
</Container>
</Provider>
</div>
);
}
export default App;
Context implementation is easy, you only need to understand its flow and how to update the root values.
You have done everything right, and I believe all that is left is for you to update the Details component on click of the Card component. Here's how you could go about it.
In the Card component, where you've handled the onClick event:
<Card.Body onClick={UpdateDetails}>, change it to: <Card.Body onClick={() => UpdateDetails(player)}>
This will help us send the data of the card we want to show in the Details component.
Inside the context file, in the UpdateDetails method, collect the argument passed while calling the function and set the currentPlayer accordingly like this:
const UpdateDetails = (player) => setCurrentPlayer(player)
Here, the context will get updated and wherever you use the currentPlayer value will receive the new data.
You also need the currentPlayer state inside Details file, import it along with players state:
const {players, currentPlayer} = useContext(ApiContext)
Now you can use currentPlayer.name or any other key from the player object.
Hope this helps. Please accept this answer if it does! Thank you!
I'm writing a swiper pagination but i faced to this error:
'swiper/css/pagination' in 'C:\react\project1\src\Components\Testimonials'
and it does not show the pagination in browser
can someone help me to solve it?
react
enter code here
import { Swiper, SwiperSlide } from "swiper/react";
import 'swiper/swiper-bundle.min.css'
import 'swiper/swiper.min.css'
import { Pagination } from 'swiper';
import 'swiper/css/pagination';
const Testimonial = () => {
const clients = []
{/* slider */}
<Swiper
modules = {[Pagination]}
slidesPerView = {1}
Pagination = {{clickable: true}}
>
{clients.map((client, index) => {
return(
<SwiperSlide key={index}>
<div className="testimonial">
<img src={client.img} alt="" />
<span>{client.review}</span>
</div>
</SwiperSlide>
)
})}
</Swiper>
</div>
)
}
**I have problem with react-swipper when i'm trying to separate it into smaller components.
Below is my full code from one component which i want to split from this:
import { Swiper } from 'swiper/react/swiper-react';
import { SwiperSlide } from 'swiper/react/swiper-react';
import { Pagination } from 'swiper';
import 'swiper/swiper.scss';
<div className={styles['categories-showcase']}>
<Swiper
slidesPerView={4}
spaceBetween={200}
grabCursor={true}
pagination={{
clickable: true,
}}
modules={[Pagination]}
>
{categories.map(category => {
return (
<SwiperSlide>
<div className={styles['categories-showcase__item']}>
<img
className={styles['swiper-image']}
src={category.image}
alt={category.name}
/>
</div>
</SwiperSlide>
);
})}
</Swiper>
</div>
into this:
<SwiperSlider>
{categories.map(c => (
<Slide key={c.id} {...c} />
))}
</SwiperSlider>
And here what i've tried:
SwiperSlider - pass multiple slides as children of JSX.Element[]
import React from 'react';
import { Swiper, SwiperSlide } from 'swiper/react/swiper-react';
import { Pagination } from 'swiper';
import 'swiper/swiper.scss';
import styles from './swiper-slider.module.scss';
interface IProps {
children: JSX.Element[];
}
const SwiperSlider = ({ children }: IProps) => {
return (
<div className={styles['showcase']}>
<Swiper
slidesPerView={4}
spaceBetween={200}
grabCursor={true}
pagination={{
clickable: true,
}}
modules={[Pagination]}
>
{children}
</Swiper>
</div>
);
};
export default SwiperSlider;
Slide which takes category as prop
import { ICategory } from 'models/category';
import React from 'react';
import { SwiperSlide } from 'swiper/react/swiper-react';
import 'swiper/swiper.scss';
import styles from './slide.module.scss';
const Slide = (category: ICategory) => {
return (
<SwiperSlide>
<div className={styles['showcase-item']}>
<img className={styles['swiper-image']} src={category.image} alt={category.name} />
</div>
</SwiperSlide>
);
};
export default Slide;
When i'm doing it this way, then my swiper doesn't work properly. It is displayed in one vertical row, when it should be displayed horizontally.
Could you tell me what i'm doing wrong here?
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/kMTMq.png**
I think you should iterate over your array on SwiperSlider and render your custom Slide which export SwiperSlide
const SwiperSlider = ({ children }: IProps) => (
<div className={styles['showcase']}>
<Swiper
slidesPerView={4}
spaceBetween={200}
grabCursor={true}
pagination={{
clickable: true,
}}
modules={[Pagination]}
>
{categories.map((category) => <Slide category={category} /> )}
</Swiper>
</div>
);
export default SwiperSlider;
I have a component called Component1 in which I have the following code:
import React, { useState } from "react";
import Popover from "material-ui-popup-state/HoverPopover";
import Fab from "#material-ui/core/Fab";
import {
usePopupState,
bindHover,
bindPopover
} from "material-ui-popup-state/hooks";
import PaletteIcon from "#material-ui/icons/Palette";
import Colors from "./Colors";
const DEFAULT_COLOR = "red";
const COLORS = [/*list of colors*/];
const Component1 = ({ classes }) => {
const popupState = usePopupState({
variant: "popover",
popupId: "demoPopover"
});
const [selectedColor, setSelectedColor] = useState(DEFAULT_COLOR);
return (
<div className="box" style={{ backgroundColor: selectedColor }}>
<Fab variant="extended" {...bindHover(popupState)}>
<PaletteIcon />
</Fab>
<Popover
>
<div className="color-palette">
{COLORS.map((color) => (
<Colors
key={color}
selected={selectedColor === color}
onClick={setSelectedColor}
color={color}
/>
))}
</div>
</Popover>
</div>
);
};
export default Component1;
This component is imported in Component2 where the code is:
import React from "react";
import Component1 from "./Component1";
import Fab from "#material-ui/core/Fab";
import DeleteIcon from "#material-ui/icons/Delete";
function Component2(props) {
function handleClick() {
props.onDelete(props.id);
}
return (
<div className="note" style={{ backgroundColor: "selectedColor" }}>
<h1>{props.title}</h1>
<p>{props.content}</p>
<Fab onClick={handleClick}>
<DeleteIcon fontSize="small" />
</Fab>
<HoverPopover />
</div>
);
}
export default Component2;
In component2 I need to use the const selectedColor for styling purpose for div with class="note". However the issue is when I select colors from COLORS list the background-color of div with class="note" is not changing. I tried many options but I don't understand how to do it correctly. Please tell me how to do it right.
To share the "selectedColor" variable, which is actually a state, you would have to pass it through the props to the child component
Your "Component2" should declare the state "selectedColor", and this state and its function must be passed by the props to your "Component1".
https://reactjs.org/tutorial/tutorial.html#lifting-state-up