Defining MongoDB Image Array in Next JS - arrays

in the following code, how do I define itemData as post.images[i]?
import clientPromise from "../lib/mongodb";
import Box from '#mui/material/Box';
import Avatar from '#mui/material/Avatar';
import IconButton from '#mui/material/IconButton';
import ChatBubbleOutlineRoundedIcon from '#mui/icons-material/ChatBubbleOutlineRounded';
import FavoriteBorderRoundedIcon from '#mui/icons-material/FavoriteBorderRounded';
import ShareRoundedIcon from '#mui/icons-material/ShareRounded';
import Button from '#mui/material/Button';
import ImageList from '#mui/material/ImageList';
import ImageListItem from '#mui/material/ImageListItem';
export default function Posts({ posts }) {
return (
<div>
<ul>
<div style={{ width: '100%' }}>
<Box sx={{ display: 'flex', justifyContent: 'center'}}>
<div style={{ width: '75%' }}>
{posts.map((post) => (
<li style={{ border: '1px solid grey', borderRadius: '12px', margin: "10px", padding: "10px" }}>
<Box sx={{ display: 'flex', justifyContent: 'start', alignItems: 'center'}}>
<Avatar alt={post.userName} src={post.userAvatar}/>
<h4 style={{ paddingLeft:'20px', paddingRight: '20px' }}>{post.userName}</h4>
<p style={{ color: 'grey', fontSize: '12px' }}>{post.timestamp}</p>
</Box>
<Box>
<p style={{ display: 'block', width: '100%', margin: '0'}}>{post.text}</p>
</Box>
<Box sx={{ my: 1, display: 'flex', justifyContent: 'end', alignItems: 'center'}}>
<IconButton><ChatBubbleOutlineRoundedIcon/></IconButton>
<IconButton><FavoriteBorderRoundedIcon/></IconButton>
<IconButton><ShareRoundedIcon/></IconButton>
</Box>
<Box><Button href={post.link} variant="outlined">Link</Button></Box>
<Box>
<ImageList sx={{ width: 500, height: 450 }} cols={3} rowHeight={164}>
{itemData.map((item) => (
<ImageListItem key={item.img}>
<img
src={`${item.img}?w=164&h=164&fit=crop&auto=format`}
srcSet={`${item.img}?w=164&h=164&fit=crop&auto=format&dpr=2 2x`}
alt={item.title}
loading="lazy"
/>
</ImageListItem>
))}
</ImageList>
</Box>
</li>
))}
</div>
</Box>
</div>
</ul>
</div>
)
}
const itemData = [
{
img: 'https://images.unsplash.com/photo-1523217582562-09d0def993a6',
title: 'House',
},
{
img: 'https://images.unsplash.com/photo-1615874694520-474822394e73',
title: 'Living Room',
},
{
img: 'https://images.unsplash.com/photo-1616594039964-ae9021a400a0',
title: 'Bedroom',
},
{
img: 'https://images.unsplash.com/photo-1616486886892-ff366aa67ba4',
title: 'Dining Room',
},
{
img: 'https://images.unsplash.com/photo-1521783593447-5702b9bfd267',
title: 'Bathroom',
}
];
export async function getServerSideProps() {
try {
const client = await clientPromise;
const db = client.db("abode-app");
const posts = await db
.collection("posts")
.find({})
.sort({ metacritic: -1 })
.limit(20)
.toArray();
return {
props: { posts: JSON.parse(JSON.stringify(posts)) },
};
} catch (e) {
console.error(e);
}
}
I have tried loading the images one by one and they work, but prefer to loop over them.

Related

How to set data in modal in nextjs

I'm having trouble getting the data and displaying it on the modal interface when editing, usually I will use useState to update the data but is there any way I can pass the data through the variable?.thanks for your help !
view
Here is my code:
this is my index,
I'm trying to get the name and tags back and show the edit form
this is my index
import type { NextPage } from 'next'
import { useEffect, useState } from 'react'
import {
Box,
Button,
Grid,
Icon,
IconButton,
Menu,
MenuItem,
Typography,
} from '#mui/material'
import { MainLayout } from 'components/Layout/MainLayout'
import { HideAppBar } from '../AppBar/HideAppBar'
import { TagProps } from './components/Tag'
import { TraitProps } from './components/Trait'
import { TraitsPickerModal } from './components/TraitsPickerModal'
import { CreateTagCategoryModal } from './components/CreateTagCategoryModal'
import { ALL_TAG, TagCategoryList } from './components/AllTag'
import { Trait } from './components/Trait'
import EditOutlinedIcon from '#mui/icons-material/EditOutlined'
import DeleteOutlinedIcon from '#mui/icons-material/DeleteOutlined'
import { useStaticTagsViewModelSWR } from './hooks/useStaticArtViewModelSWR'
import { TagCategoryEditModal } from './components/Modal/TagCategoryEditModal'
import { TagCategoryDeleteModal } from './components/Modal/TagCategoryDeleteModal'
import { BlackButton } from 'components/Elements/Button/BlackButton'
import { MaterialIconFont } from 'assets/Styles/GlobalStyles'
import {
useStaticSWRTagsListViewModel,
StaticSWRTagstListKey,
} from './hooks/useStaticSWRTagsListViewModel'
import { mutate } from 'swr'
import { TagCategoryListData } from './interface'
const CONTENT_PADDING = '16px'
const dummyTagCategoryList = [
{
name: 'testes',
tagList: [
{
name: 'niceTag',
traitList: [
{
category: 'Clothes',
name: 'Polka',
icon: '/images/test/body4.png',
},
{
category: 'Clothes',
name: 'Dress',
icon: '/images/test/body5.png',
},
],
},
{ name: 'badTag', traitList: [] },
],
},
{
name: 'cat2',
tagList: [
{ name: 'tag1', traitList: [] },
{ name: 'tag2', traitList: [] },
],
},
]
type TaggedListType = TraitProps[][][]
type UntaggedListType = Record<string, TraitProps[]>[]
type TagCategoryListKey = keyof TagCategoryList
const Tags: NextPage = () => {
/**
* state
*/
const [tagCategoryList, setTagCategoryList] = useState<TagProps[]>([])
const [createModalOpen, setCreateModalOpen] = useState(false)
const [traitsPickerOpen, setTraitsPickerOpen] = useState(false)
const [categoryIndex, setCategoryIndex] = useState(0)
const [tagIndex, setTagIndex] = useState(0)
const [taggedList, setTaggedList] = useState<TaggedListType>([])
const [unTaggedList, setUntaggedList] = useState<UntaggedListType>([{}])
const [isVisibleUntaggedList, setIsVisibleUntaggedList] = useState<boolean[]>(
[...new Array(tagCategoryList.length)].map((n) => Boolean(n))
)
const [categoryAnchorElList, setCategoryAnchorElList] = useState(
[...new Array(tagCategoryList.length)].map(() => null)
)
const [categoryAnchorEl, setCategoryAnchorEl] = useState<null | HTMLElement>(
null
)
const [tagCategoryListData, setTagCategoryListData] = useState<
TagCategoryListData[]
>([])
const categoryMenuOpen = Boolean(categoryAnchorEl)
const [idItemTag, setIdItemTag] = useState(0)
/**
* Hook
*/
const { viewModel, setViewModel } = useStaticTagsViewModelSWR()
const { viewModelTagCategory, setViewModelTagCategory } =
useStaticSWRTagsListViewModel()
/**
* useEffect
*/
useEffect(() => {
setTagCategoryList(dummyTagCategoryList)
}, [])
useEffect(() => {
mutate(StaticSWRTagstListKey)
}, [])
useEffect(() => {
setTagCategoryListData(viewModelTagCategory)
}, [viewModelTagCategory])
useEffect(() => {
setTaggedList((list) =>
Object.assign(
tagCategoryList.map((category) => {
return category.tagList.reduce((memo, tag) => {
return tag.traitList
? [...memo, tag.traitList.map((trait) => trait)]
: memo
}, [])
}),
list
)
)
setIsVisibleUntaggedList((list) => [...list, false])
}, [tagCategoryList])
useEffect(() => {
const categoryUsedTraitList = taggedList.reduce((memo, list) => {
return [...memo, list.flat(2)]
}, [])
setUntaggedList(
categoryUsedTraitList.map((list) => {
return Object.keys(ALL_TAG).reduce((memo, key: TagCategoryListKey) => {
memo[key] = ALL_TAG[key].filter((trait) => {
return list.every((item) => {
return item.name !== trait.name
})
})
return memo
}, {} as TagCategoryList)
})
)
}, [taggedList])
/**
* Handler
*/
const onClickTrait = (pickedList: TraitProps[]) => {
setTaggedList((list) => {
list[categoryIndex][tagIndex] = pickedList
return JSON.parse(JSON.stringify(list))
})
}
const onClickToggleUntaggedComponentsBtn = (index: number) => {
setIsVisibleUntaggedList((list) => {
list[index] = !list[index]
return [...list]
})
}
const addTagCategory = (tagCategory: TagCategoryListData) => {
setViewModelTagCategory([...viewModelTagCategory, tagCategory])
}
const menuClick = (
event: React.MouseEvent<HTMLElement>,
idItemTag: number
) => {
setIdItemTag(idItemTag)
setCategoryAnchorEl(event.currentTarget)
}
const menuClose = () => {
setCategoryAnchorEl(null)
}
const openTagCategoryEditModal = (idItemTag: number) => {
const item = viewModelTagCategory.find((tagId) => tagId.id === idItemTag)
console.log(item?.name)
const itemTags = item?.tagList.map((tag) => {
return {
id: tag.id,
name: tag.name,
}
})
console.log(itemTags)
setViewModel({ ...viewModel, tagCategoryEditModal: { isOpen: true } })
setCategoryAnchorEl(null)
}
const openTagCategoryDeleteModal = (idItemTag: number) => {
console.log(idItemTag)
setViewModel({ ...viewModel, tagCategoryDeleteModal: { isOpen: true } })
setCategoryAnchorEl(null)
}
return (
<MainLayout>
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
<HideAppBar tabNum={2} />
<MaterialIconFont />
<TagCategoryEditModal />
<TagCategoryDeleteModal />
<Grid
container
sx={{
position: 'relative',
display: 'block',
textAlign: 'left',
padding: CONTENT_PADDING,
}}
>
<Typography
variant="h5"
style={{
textAlign: 'left',
fontWeight: 'bold',
}}
>
Tags
</Typography>
<Box sx={{ display: 'flex' }}></Box>
<Typography
variant="h6"
style={{ color: '#888888', textAlign: 'left' }}
>
Use tags to organize and group your token traits.
</Typography>
<BlackButton
onClick={() => setCreateModalOpen(true)}
sx={{
position: 'absolute',
right: CONTENT_PADDING,
top: CONTENT_PADDING,
paddingRight: '16px',
fontWeight: 'bold',
fontSize: '0.9rem',
}}
>
<Icon sx={{ marginRight: '8px' }}>add</Icon>New Tag Category
</BlackButton>
{viewModelTagCategory && (
<Box>
{viewModelTagCategory.map((tagCategory, i) => (
<Box key={tagCategory.name + i}>
<Box className="category-box" sx={{ marginTop: '16px' }}>
<Box sx={{ display: 'flex', alignItems: 'center' }}>
<Typography
variant="subtitle1"
sx={{ fontWeight: 'bold' }}
>
{tagCategory.name}_{tagCategory.id}
</Typography>
<IconButton
aria-label="more"
id={`tag-category-menu-btn-${tagCategory.id}`}
aria-controls={
categoryMenuOpen
? `tag-category-menu-${tagCategory.id}`
: undefined
}
aria-expanded={categoryMenuOpen ? 'true' : undefined}
aria-haspopup="true"
data-category-num={tagCategory.id}
onClick={(e) => menuClick(e, tagCategory.id)}
>
<Icon>more_vert</Icon>
</IconButton>
<Menu
id={`tag-category-menu-${tagCategory.id}`}
MenuListProps={{
'aria-labelledby': `tag-category-menu-btn-${tagCategory.id}`,
}}
anchorEl={categoryAnchorEl}
open={categoryMenuOpen}
onClose={menuClose}
>
<MenuItem
onClick={() => openTagCategoryEditModal(idItemTag)}
>
<EditOutlinedIcon sx={{ mr: 1 }} />
<Typography>Edit</Typography>
</MenuItem>
<MenuItem
onClick={() => openTagCategoryDeleteModal(idItemTag)}
sx={{ color: 'red' }}
>
<DeleteOutlinedIcon sx={{ mr: 1 }} />
<Typography>Delete</Typography>
</MenuItem>
</Menu>
</Box>
{tagCategory.tagList.length > 0 &&
tagCategory.tagList.map((tag, j) => (
<Box key={j}>
<Box sx={{ display: 'flex', marginTop: '32px' }}>
<Box
sx={{
display: 'flex',
alignItems: 'center',
background: '#283237',
color: '#ffffff',
padding: '0 0 0 12px',
borderRadius: '20px',
fontWeight: 'bold',
marginLeft: '8px',
':first-of-type': {
marginLeft: 0,
},
}}
>
{tag.name}
<IconButton
size="small"
sx={{ color: '#ffffff' }}
>
<Icon fontSize="small">close</Icon>
</IconButton>
</Box>
<Button
sx={{
display: 'flex',
alignItems: 'center',
background: '#ffffff',
color: '#000000',
padding: '0 12px',
borderRadius: '20px',
fontWeight: 'bold',
border: '#aaaaaa 1px solid',
marginLeft: '8px',
textTransform: 'none',
':hover': {
borderColor: '#1976d2',
boxShadow: '0px 0px 0px 1px #1976d2 inset',
},
':only-child': {
marginLeft: 0,
},
}}
onClick={() => {
setCategoryIndex(i)
setTagIndex(j)
setTraitsPickerOpen(true)
}}
>
Add Traits
</Button>
</Box>
{taggedList[i] && (
<Box
sx={{
display: 'flex',
flexWrap: 'wrap',
marginTop: '8px',
width: '1000px',
}}
>
{taggedList[i][j].map((trait, k) => (
<Trait key={k} {...trait} />
))}
</Box>
)}
</Box>
))}
</Box>
<Button
onClick={() => onClickToggleUntaggedComponentsBtn(i)}
sx={{
fontSize: '0.85rem',
marginTop: '40px',
color: '#005eff',
textTransform: 'none',
}}
>
{isVisibleUntaggedList[i] ? 'Hide' : 'Show'} untagged
components
</Button>
{isVisibleUntaggedList[i] && (
<Box sx={{ width: '1000px', maxWidth: '100vw' }}>
{unTaggedList[i] &&
Object.keys(unTaggedList[i]).map((key, k) => (
<Box key={k}>
{unTaggedList[i][key].length > 0 && (
<>
<Box sx={{ fontWeight: 'bold' }}>{key}</Box>
<Box
sx={{
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap',
}}
>
{unTaggedList[i][key].map((trait, l) => (
<Trait key={`${k}_${l}`} {...trait} />
))}
</Box>
</>
)}
</Box>
))}
</Box>
)}
</Box>
))}
</Box>
)}
<CreateTagCategoryModal
open={createModalOpen}
setOpen={setCreateModalOpen}
onCreate={addTagCategory}
/>
<TraitsPickerModal
title={
tagCategoryList[categoryIndex]
? `${tagCategoryList[categoryIndex].tagList[tagIndex].name} Traits`
: ''
}
open={traitsPickerOpen}
setOpen={setTraitsPickerOpen}
onPick={onClickTrait}
traitList={
taggedList[categoryIndex]
? taggedList[categoryIndex][tagIndex]
: []
}
/>
</Grid>
</Box>
</MainLayout>
)
}
export default Tags
My modalEdit
import * as React from 'react'
import {
Box,
Button,
Modal,
Typography,
IconButton,
TextField,
FormControlLabel,
Switch,
} from '#mui/material'
import { ModalBox } from 'components/Elements/Box/ModalBox'
import { useStaticTagsViewModelSWR } from '../../hooks/useStaticArtViewModelSWR'
import CloseIcon from '#mui/icons-material/Close'
import AddCircleOutlineIcon from '#mui/icons-material/AddCircleOutline'
import DeleteOutlineOutlinedIcon from '#mui/icons-material/DeleteOutlineOutlined'
export const TagCategoryEditModal = () => {
const { viewModel, setViewModel } = useStaticTagsViewModelSWR()
const [includeMetadataFlag, setIncludeMetadataFlag] =
React.useState<boolean>(true)
const modalClose = () => {
setViewModel({ ...viewModel, tagCategoryEditModal: { isOpen: false } })
}
return (
<Modal
open={viewModel.tagCategoryEditModal.isOpen}
onClose={modalClose}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
>
<>
<ModalBox
style={{ display: 'flex', flexDirection: 'column', height: 450 }}
>
<Box
sx={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
mb: 2,
}}
>
<Typography variant="h4" sx={{ fontWeight: 'bold' }} gutterBottom>
Edit Tag Category
</Typography>
<IconButton onClick={modalClose}>
<CloseIcon />
</IconButton>
</Box>
<Box sx={{ mb: 3 }}>
<Typography sx={{ mb: 1 }}>Name</Typography>
<TextField sx={{ width: '100%' }} />
</Box>
<Box sx={{ mb: 3 }}>
<Box
sx={{
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
}}
>
<Typography>Tags</Typography>
<IconButton>
<AddCircleOutlineIcon />
</IconButton>
</Box>
<Box
sx={{
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
}}
>
<TextField sx={{ width: '100%', mr: 2 }} />
<IconButton>
<DeleteOutlineOutlinedIcon />
</IconButton>
</Box>
</Box>
<Box sx={{ mb: 2 }}>
<FormControlLabel
sx={{ ml: 0 }}
value="start"
control={
<Switch
color="primary"
checked={includeMetadataFlag}
onChange={() => setIncludeMetadataFlag(!includeMetadataFlag)}
/>
}
label="Include in metadata?"
labelPlacement="start"
/>
</Box>
<Box sx={{ display: 'flex', justifyContent: 'flex-end' }}>
<Button
variant="outlined"
sx={{ color: 'black', borderColor: '#d1d8db', marginRight: 1 }}
onClick={modalClose}
>
Cancel
</Button>
<Button variant="contained">Save</Button>
</Box>
</ModalBox>
</>
</Modal>
)
}

Each child in a list should have a unique "key" prop Error for custom component

I am mapping over an array and rendering a custom card component for each index in the array. However, I am receiving the error "Each child in a list should have a unique "key" prop" 1. Although, I am passing the index as the key. I have tried with a React.fragment and passing the index down to the card component and adding the key there. Both methods are still throwing the same error.
Main Component
import React from "react";
import { useRouter } from "next/router";
import { Button, Container } from "#mui/material";
import { makeStyles } from "#mui/styles";
import { InstructionsCard } from "../layout/directory";
import {
RiNumber1 as OneIcon,
RiNumber2 as TwoIcon,
RiNumber3 as ThreeIcon,
} from "react-icons/ri";
function InstructionSection() {
const router = useRouter();
const classes = useStyles();
const instructions = [
{
id: 1,
icon: OneIcon,
title: "step one",
text: [
"Navigate to the",
<Button
onClick={() => router.push("/requirements")}
size="small"
style={{ margin: "5px" }}
variant="outlined"
color="inherit"
>
requirements
</Button>,
"page for our most frequently asked questions and specific requirements before booking any activity. ",
],
},
{
id: 2,
icon: TwoIcon,
title: "step two",
text: [
"Find the activity you are interested in and read through the information carefully. Be sure to fully understand the,",
<Button
onClick={() => router.push("/#upgrades")}
size="small"
style={{ margin: "5px" }}
variant="outlined"
color="inherit"
>
entry fee
</Button>,
" and",
<Button
onClick={() => router.push("/#upgrades")}
size="small"
style={{ margin: "5px" }}
variant="outlined"
color="inherit"
>
upgrade
</Button>,
" packages",
],
},
{
id: 3,
icon: ThreeIcon,
title: "step three",
text: [
"Please, be sure to verify we are ",
<Button
onClick={() => router.push("/locations")}
size="small"
style={{ margin: "5px" }}
variant="outlined"
color="inherit"
>
located
</Button>,
" in your area. Select an experience, date, time-slot, toggle any upgrades, and continue through checkout.",
],
},
];
return (
<Container className={classes.root}>
{/* instructions iteration */}
{instructions.map((_instruction, index) => {
return (
<React.Fragment key={index}>
<InstructionsCard item={_instruction} />
</React.Fragment>
);
})}
</Container>
);
}
// custom styles
const useStyles = makeStyles((theme) => ({
root: {
[theme.breakpoints.down("md")]: {
flexDirection: "column",
},
width: "100%",
display: "flex",
justifyContent: "space-evenly",
},
}));
export default InstructionSection;
Card Component
import { makeStyles } from "#mui/styles";
import { Card, CardContent, Typography, Divider } from "#mui/material";
const InstructionsCard = ({ item }) => {
const classes = useStyles();
const Icon = item.icon;
return (
<Card className={classes.root}>
<CardContent>
<Icon className={classes.icon} />
<Typography className={classes.title} variant="h5" component="h6">
{item.title.toUpperCase()}
</Typography>
<Divider className={classes.divider} />
<Typography
variant="subtitle1"
component="p"
sx={{ mb: 1.5 }}
color="text.secondary"
>
{item.text}
</Typography>
</CardContent>
</Card>
);
};
const useStyles = makeStyles((theme) => ({
root: {
[theme.breakpoints.down("md")]: {
margin: theme.spacing(4, 0, 4, 0),
},
background: theme.palette.primary.main,
borderRadius: theme.spacing(5),
padding: theme.spacing(2),
margin: theme.spacing(5),
width: "100%",
textAlign: "center",
boxShadow: `0px 0px 10px 10px ${theme.palette.offset.main}`,
},
icon: {
background: theme.palette.secondary.dark,
width: "50px",
height: "50px",
padding: "15px",
borderRadius: theme.spacing(5),
},
divider: {
background: theme.palette.secondary.dark,
padding: "2px",
width: "20%",
margin: theme.spacing(1, "auto", 1, "auto"),
},
title: {
fontWeight: 800,
},
}));
export default InstructionsCard;
Change like this in your main component
React.Fragment we need to use One time it can't use multiple time
return (
<Container className={classes.root}>
<React.Fragment>
{instructions.map((_instruction, index) => {
<InstructionsCard key={index} item={_instruction} />;
})}
</React.Fragment>
</Container>
);
Thank You

split material ui style to other file

I wish to remove all the styles from my main component.
All the JS style of the material UI is on the same file and it is starting to get long code.
I wish to create a new file that contains all the styles on other file and on Navbar.js I will just call the components, just same as CSS file that I call to class
import styled from "#emotion/styled";
import { Notifications, Pets } from "#mui/icons-material";
import {
AppBar,
Avatar,
Badge,
Box,
InputBase,
Menu,
MenuItem,
Toolbar,
Typography,
} from "#mui/material";
import MailIcon from "#mui/icons-material/Mail";
import React, { useState } from "react";
const StyledToolbar = styled(Toolbar)({
display: "flex",
justifyContent: "space-between",
});
const Search = styled("div")(({ theme }) => ({
backgroundColor: "white",
padding: "0 10px",
borderRadius: theme.shape.borderRadius,
width: "40%",
}));
const Icons = styled(Box)(({ theme }) => ({
display: "none",
gap: "20px",
alignItems: "center",
[theme.breakpoints.up("sm")]: {
display: "flex",
},
}));
const UserBox = styled(Box)(({ theme }) => ({
display: "flex",
gap: "10px",
alignItems: "center",
[theme.breakpoints.up("sm")]: {
display: "none",
},
}));
const Navbar = () => {
const [open, setOpen] = useState(false);
return (
<AppBar position="sticky">
<StyledToolbar>
<Typography variant="h6" sx={{ display: { xs: "none", sm: "block" } }}>
PALSAM
</Typography>
<Pets sx={{ display: { xs: "block", sm: "none" } }} />
<Search>
<InputBase placeholder="search..." />
</Search>
<Icons>
<Badge badgeContent={4} color="error">
<MailIcon />
</Badge>
<Badge badgeContent={4} color="error">
<Notifications />
</Badge>
<Avatar
sx={{ width: 30, height: 30 }}
src="https://images.pexels.com/photos/846741/pexels-photo-846741.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2"
onClick={(e) => setOpen(true)}
/>
</Icons>
<UserBox onClick={(e) => setOpen(true)}>
<Avatar
sx={{ width: 30, height: 30 }}
src="https://images.pexels.com/photos/846741/pexels-photo-846741.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2"
/>
<Typography variant="span">Jhon</Typography>
</UserBox>
</StyledToolbar>
<Menu
id="demo-positioned-menu"
aria-labelledby="demo-positioned-button"
open={open}
onClose={(e) => setOpen(false)}
anchorOrigin={{
vertical: "top",
horizontal: "right",
}}
transformOrigin={{
vertical: "top",
horizontal: "right",
}}
>
<MenuItem>Profile</MenuItem>
<MenuItem>My account</MenuItem>
<MenuItem>Logout</MenuItem>
</Menu>
</AppBar>
);
};
export default Navbar;
You can create a new js file and add export before each const then you can import them from that file
export const StyledToolbar = styled(Toolbar)({
display: "flex",
justifyContent: "space-between",
});

Image not showing While Using React-Select Dropdown Select Option

I am trying to implement a select dropdown option with image by the left side of the text. I used react-select library but the image shows broken.
Here is my code:
import React from 'react'
import Select from 'react-select'
import { components } from 'react-select';
import { login } from "../customSelect/login.png";
const options = [
{
label: 'Option 1',
value: 0,
image: "../customSelect/login.png",
},
{
label: 'Option 2',
value: 1,
image: "../customSelect/login.png",
}
];
const customStyles = {
option: (provided) => ({
...provided,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
}),
singleValue: (provided) => ({
...provided,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
}),
}
const { SingleValue, Option } = components;
const IconSingleValue = (props) => (
<SingleValue {...props}>
<img src={props.data.image}
style={{ height: '30px', width:
'30px', borderRadius: '50%', marginRight: '10px' }}/>
{props.data.label}
</SingleValue>
);
const IconOption = (props) => (
<Option {...props}>
<img src={props.data.image}
style={{ height: '30px', width: '30px', borderRadius: '50%',
marginRight: '10px' }}/>
{props.data.label}
</Option>
);
const SelectOption = () => {
return (
<div>
<Select
styles={customStyles}
components={{SingleValue:IconSingleValue,
Option:IconOption}}
options={options}
/>
</div>
)
}
export default SelectOption
this is the result i got

How to import just one function from different file into other?

I have a layout file where I made footer and navigation and I insert these two functions in Layout const (code below). In the new file, I just need the Navigation function so how I can insert it without a footer? Because when I write in my new file import Navigation from "../components/layout" and the in code insert I've got error...
const Layout = ({ children }) => {return (
<div>
<Navigation></Navigation>
<Global
styles={{
html: {
backgroundColor: "#fff",
color: "#111",
fontFamily: `'Poppins', sans-serif`,
fontSize: 14,
[Screen.S]: {
fontSize: 16,
},
[Screen.M]: {
fontSize: 18,
},
[Screen.L]: {
fontSize: 20,
},
},
a: {
color: "unset",
},
}}
/>
{children}
<Footer></Footer>
</div>
)
}
function Navigation() { const [navbarOpen, setNavbarOpen] = useState(false) return (
<header
css={{
width: "100%",
maxWidth: "100%",
padding: "0 24px",
position: "fixed",
background: "#fff",
boxShadow: "0 0 0.35rem rgba(0,0,0,.25)",
zIndex: "100",
top: 0,
}}
>
<div
css={{
gridAutoFlow: "column",
minHeight: "4.5rem",
display: "grid",
maxWidth: 1200,
margin: "0 auto",
gridTemplateColumns: "auto 1fr",
alignItems: "center",
paddingLeft: 35,
}}>
<Link to="/ ">
<img style={{ height: "2.5rem" }} src={logo}/>
</Link>
<Toggle
navbarOpen={navbarOpen}
onClick={() => setNavbarOpen(!navbarOpen)}
>
{navbarOpen ? <Hamburger open /> : <Hamburger />}
</Toggle>
{navbarOpen ? (
<NavBoxIcons>
<NavbarSocialLinks />
</NavBoxIcons>
) : (
<NavBox open>
<div>
<HeaderLink>About</HeaderLink>
<HeaderLink>Blog</HeaderLink>
</div>
<div>
<NavbarLinks />
</div>
</NavBox>
)
}
</div>
</header >
)
}
function Footer() { return (
<footer
css={{
padding: "6rem 2rem",
fontSize: "1rem",
minHeight: 160,
fontFamily: "sans-serif",
...Css.container,
}}
>
<div
css={{
display: "flex",
flexDirection: "column",
marginBottom: "3.6rem",
}}
>
<div
css={{
fontSize: "1.2rem",
display: "grid",
gridGap: "0.8rem",
}}>
<a>
<span>Privacy police</span>
</a>
</div>
</div>
<div
css={{
display: "grid",
gridTemplateColumns: "1fr auto",
alignItems: "center",
fontWeight: "lighter",
}}>
<div css={{ display: "flex", flexDirection: "column" }}>
<span>My Page</span>
</div>
</div>
</footer>
)
}
try exporting both Navigation and Footer like this
//at bottom of file
export {Navigation, Footer}
import individual component like this
import {Navigation} from 'components/layout'
or
import {Navigation,Footer} from 'components/layout'
lookup exporting in js

Resources