I put the key prop to parent component but it gives unique key error. Even I put the key props to every element still getting this error. How can I fix this problem?
{orders &&
orders.slice(0).reverse().map((order) =>
order.order_type === 'PickupOrders' &&
<Row key={uuidv4()} align="middle" style={{ height: '6vh', borderBottom: '1px solid #e6ebe7' }} justify="center" >
<Col span={9} key={uuidv4()} >
<p key={uuidv4()} style={{ marginBottom: 'auto', marginTop: 'auto' , textAlign:'center'}}>{order.customer_fullname}</p>
</Col>
<Col span={9} key={uuidv4()}>
<p key={uuidv4()} style={{ marginBottom: 'auto', marginTop: 'auto' , textAlign:'center'}} >{order.order_status}</p>
</Col>
<Col span={6} key={uuidv4()}>
<p key={uuidv4()} style={{ marginBottom: 'auto', marginTop: 'auto' , textAlign:'center'}} >{order.total_price}</p>
</Col>
</Row>
)}
{counter > 0 &&
_.times( counter , (i) => (
<div style={{ height: '6vh', borderBottom: '1px solid #e6ebe7' }} >
<Dropdown
key={uuidv4()}
overlay={menu( PickUpOrder(orders, counter - i), openModalFunc)}
placement='bottomCenter'
trigger={['click']}
>
<IconButton
color='primary'
aria-label='add an alarm'
>
<MoreVertIcon />
</IconButton>
</Dropdown>
</div>
))
}
The first part looks fine. When you map or _.times in order to iterate arrays or array of objects, key attribute should be in top tag. In your case;
_.times( counter , (i) => (
<div style={{ height: '6vh', borderBottom: '1px solid #e6ebe7' }} key={uuidv4()}> {/* <---- */}
<Dropdown
overlay={menu( PickUpOrder(orders, counter - i), openModalFunc)}
placement='bottomCenter'
trigger={['click']}
> ...
I hope it works fine.
Related
I get this error even though I put a key.
react_devtools_backend.js:4026 Warning: Each child in a list should have a unique "key" prop.
Shows here a picture of the error
Showing the code here :
return (
<div>
<div style={{ textAlign: "center" }}>
{players?.length > 0 && (
<div style={{ marginTop: "2rem" }}>
<h2>Results "{query}"</h2>
<TableContainer
component={Paper}
style={{
marginLeft: "auto",
marginRight: "auto",
width: "90%",
}}
>
<Table sx={{ minWidth: 350 }} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell>Full Name</TableCell>
<TableCell>Club</TableCell>
<TableCell>Nation</TableCell>
<TableCell>Position</TableCell>
<TableCell>Age</TableCell>
<TableCell>Market Value</TableCell>
<TableCell>Favorite</TableCell>
</TableRow>
</TableHead>
<TableBody>
{players.map((item) => {
return <TableRow
key={item.id}
sx={{
"&:last-child td, &:last-child th": { border: 0 },
}}
style={{ textAlign: "center" }}
>
<TableCell>
<ListItem>
<Link
onClick={() => pickPlayer(item.id)}
style={{ textDecoration: "none" }}
to={`/player/${item.id}`}
>
{item?.img !== "https://img.a.XXX.technology/portrait/header/default.jpg?lm=1" &&
<img
src={item?.img}
alt={item?.displayName}
style={{ marginRight: "5px" , width: '2.5rem' , borderRadius: '80%' }}
/>
}
{item?.img === "https://img.a.XXX.technology/portrait/header/default.jpg?lm=1" &&
<img
src={'https://www.seekpng.com/png/full/202-2024994_profile-icon-profile-logo-no-background.png'}
alt={item?.displayName}
style={{ marginRight: "5px" , width: '3rem'}}
/>
}
<span style={{ verticalAlign: "1rem" }}>
{item?.displayName}
</span>
</Link>
</ListItem>
</TableCell>
<TableCell>
<ListItem>
<img
src={`https://tmssl.akamaized.net/images/wappen/head/${item?.imgId}.png?lm=1457423031`}
alt={item?.club}
title={item?.club}
style={{ marginRight: "2px" , width: '1.7rem' }}
/>{" "}
{item?.club}
</ListItem>
</TableCell>
<TableCell>
{item?.nationality.map((item) => {
return (
<>
<ListItem key={uuidv4()}>
<img
key={uuidv4()}
src={item?.flag}
alt={item?.title}
title={item?.title}
style={{ marginRight: "5px" , width : '1.7rem' , borderRadius : '10rem' }}
/>{" "}
{item.title}
</ListItem>
</>
);
})}
</TableCell>
<TableCell>
<ListItem>{item.position}</ListItem>
</TableCell>
<TableCell>
<ListItem>{item.age}</ListItem>
</TableCell>
<TableCell>
<ListItem>{item.tmValue}</ListItem>
</TableCell>
<TableCell>
<ListItem>
{
favoriteLoading ? "loading..." :
<>
{
favorite.find(fav => fav.playerId === item.id && fav.scoutId === auth._id) !== undefined ? <Favorite style={{ color: "red" }} /> : <FavoriteBorder style={{ color: "red" }} />
}
</>
}
</ListItem>
</TableCell>
</TableRow>
}
)}
</TableBody>
</Table>
</TableContainer>
</div>
)}
{playersLoading && players?.length === 0 ? progress : players?.length === 0 && <h3>{query} Not Found</h3> }
{playersLoading && players?.length > 0 && progress}
{playersLoading && players?.length === 0 ? null : players.length <= 3 && <div style={{height : "20vh"}}>
{
!underThree && <h4>Scroll Down to load more</h4>
}
</div>}
</div>
</div>
);
what can we do ?
I have tried until now to maybe even use an external dynamic uuid to check if the Id is repeating itself and this is not the case.
Because everything seems fine to me.
The problem is in this code:
{item?.nationality.map((item) => {
return (
<>
<ListItem key={uuidv4()}>
<img
key={uuidv4()}
src={item?.flag}
alt={item?.title}
title={item?.title}
style={{ marginRight: "5px" , width : '1.7rem' , borderRadius : '10rem' }}
/>{" "}
{item.title}
</ListItem>
</>
);
})}
The key must be on the outermost element. Your outermost element is a fragment <>, which has no key. If you don't need the fragment you can delete it and have the ListItem be the outermost element.
If you do need to have a fragment on the outside, you can use the longhand form of fragments to add a key to it:
import { Fragment } from 'react';
// ...
return (
<Fragment key={/* insert key here */}>
<ListItem>
<img
src={item?.flag}
alt={item?.title}
title={item?.title}
style={{ marginRight: "5px" , width : '1.7rem' , borderRadius : '10rem' }}
/>{" "}
{item.title}
</ListItem>
</Fragment>
)
By the way, calling uuidv4() as you're doing for the key is going to create a brand new key on every render. This is going to force every list item to unmount and remount on every render. The keys should ideally be a piece of data on the item. If that's not an option, then the index could be used as a last resort. But don't use random keys that change on every render.
Check your file where you have defined item if there is any repeated number or anything in your item.id.
Or you can also give it Math.random(); like:
{players.map((item) => {
return <TableRow
key={Math.floor(Math.random)}
...
</TableRow>
}
I have Form.list like below
before
I want label on top, like this
I wish
is it possible?
I don't know which component to use..
You can use Row & Col from antd to layout your list labels.
import { PlusOutlined } from '#ant-design/icons';
import { Button, Form, Input, Row, Col, Typography } from 'antd';
function App() {
return (
<div className='container my-4'>
<Form>
<Form.List name='names'>
{(fields, { add, remove }) => (
<>
{!!fields.length && (
<Row align='middle'>
<Col span={5} style={{ textAlign: 'center' }}>
<Typography.Text>Subnet</Typography.Text>
</Col>
<Col span={5} style={{ textAlign: 'center' }}>
<Typography.Text>Netmask</Typography.Text>
</Col>
<Col span={5} style={{ textAlign: 'center' }}>
<Typography.Text>Router</Typography.Text>
</Col>
<Col span={5} style={{ textAlign: 'center' }} />
<Col span={4} />
</Row>
)}
{fields.map((field, index) => (
<Row key={index}>
<Col span={5}>
<Form.Item {...field} style={{ margin: '5px' }}>
<Input placeholder='10.40.0.0' style={{ width: '100%' }} />
</Form.Item>
</Col>
<Col span={5}>
<Form.Item {...field} style={{ margin: '5px' }}>
<Input placeholder='255.255.255.0' style={{ width: '100%' }} />
</Form.Item>
</Col>
<Col span={5}>
<Form.Item {...field} style={{ margin: '5px' }}>
<Input placeholder='10.40.0.1' style={{ width: '100%' }} />
</Form.Item>
</Col>
<Col span={5}>
<Form.Item {...field} style={{ margin: '5px' }}>
<Input placeholder='IP Pool' style={{ width: '100%' }} />
</Form.Item>
</Col>
<Col span={4}>
<Form.Item {...field} style={{ margin: '5px' }}>
<Button onClick={() => remove(field.name)}>Delete</Button>
</Form.Item>
</Col>
</Row>
))}
<Button type='dashed' onClick={() => add()} icon={<PlusOutlined />} style={{ margin: '5px' }}>
Add Subnet
</Button>
</>
)}
</Form.List>
</Form>
</div>
);
}
export default App;
On my test branch I can get this to work just fine, but when I try to add it to my partners work, it will map just fine but when I add the .filter with the following line .includes(query.toLowerCase()) like below, The page will load for 1/2 a second and then instantly go blank. I get an error in the console saying:
"Uncaught TypeError: query.toLowerCase is not a function"
< BasicModal />
<input
autoFocus={true}
onChange={event => setQuery(event.target.value)} placeholder="Search...">
</input>
</div>
<br />
<hr />
<div>
{
wikiData.filter(data =>{
if(query === ''){
return data;
} else if (data.title.toLowerCase().includes(query.toLowerCase())) {
return data;
} else if (data.body.toLowerCase().includes(query.toLowerCase())) {
return data;
}
}).map((data, index) => (
<div key={data.id}
ref={refs[data.id]}
style={{ height: 'flex', paddingLeft: '5%', paddingRight: '5%' }}>
<h1 style={{ fontSize: 24, fontWeight: "bold", textAlign: "center" }}>{data.title}</h1>
<div style={{ textAlign: "center", paddingBottom: "10px", Top: "-5px!important" }}>
<Tooltip title="Edit Content">
<EditIcon data-id={data.id} fontSize='large' color='action' style={{ paddingRight: "10px", cursor: "pointer" }} onClick={() => handleEdit(data, index)} />
</Tooltip>
<Tooltip title="Save Edit">
<Check data-id={data.id} fontSize='large' color='success' style={{ paddingRight: "10px", cursor: "pointer" }} onClick={() => saveEdit(data, index)} />
</Tooltip>
<Tooltip title="Delete Post">
<CloseIcon fontSize='large' color='error' style={{ cursor: "pointer" }} />
</Tooltip>
</div>
<div id={data.id} className="wiki-unedited" onChange={handleChange} value={data.body} >{data.body}</div>
<hr style={{ paddingLeft: "10%", paddingRight: "10%", width: "80%" }} />
</div>
))}
</div>
</Grid>
</Grid>
</div>
I figured it out... I didnt post it all to make it solvable either.. I had the state variable set to an array instead of a string...
I'm using React-Ionic and I am not able to reduce the top/bottom padding from the Col element of my Grid.
This is the code I'm using
<IonGrid fixed={true} >
<IonRow >
{
categories.map(l => {
return (
<IonCol key={l.name} size="6" size-md >
<IonCard style={{ backgroundColor: '#444' }}>
<IonCardHeader >
<IonIcon style={{ display: 'flex', fontSize: '30px', marginBottom: 10 }} icon={l.icon} />
<IonCardTitle style={{ fontSize: 18 }}>{l.name}</IonCardTitle>
</IonCardHeader>
</IonCard>
</IonCol>
)
})
}
</IonRow>
</IonGrid>
and below you can see the result, but I wanted to have vertically closer the cards.
I also tried to change the code to
<IonCard style={{ backgroundColor: '#444', paddingTop:0, paddingBottom:0 }}>
without results.
Thanks for the help.
it is a combination of padding and margin, i reduced it to zero so you can adjust accordingly
<IonCol key={l} size="6" size-md style={{ padding : 0}}>
<IonCard style={{ backgroundColor: '#444', padding :0 , margin :0 }}>
<IonCardHeader >
<IonIcon style={{ display: 'flex', fontSize: '30px', marginBottom: 10 }} icon={homeOutline} />
<IonCardTitle style={{ fontSize: 18 }}>{l + " THIS IS TITLE"}</IonCardTitle>
</IonCardHeader>
</IonCard>
</IonCol>
I'm new to material UI framework, i want to stretch the grid accordingly displayed in the content. I couldn't able to do that. Can anyone help me in this?
<Card>
<CardHeader
avatar={<Avatar aria-label="recipe">S</Avatar>}
title={
<>
<InputBase placeholder="Search Google Maps" margin="normal" />
<IconButton type="submit" aria-label="search">
<SearchIcon />
</IconButton>
</>
}
/>
<Divider />
<CardContent
style={{ overflow: "scroll" }}
className={classes.contentHeight}
id="chatList"
>
<div>
<Message isSender content="Hello" />
{this.state.data.map(item => {
if (item.isSender) {
return (
<Message isSender name="Sanjana" content={item.content} />
);
}
return <Message name="Sanjana" content={item.content} />;
})}
</div>
</CardContent>
<Divider />
<CardActions>
<Paper className={classes.contentPaper}>
<Input
margin="dense"
className={classes.input}
placeholder="Enter a Message"
disableUnderline
/>
</Paper>
</CardActions>
</Card>
Can anyone help me in this query?
Thanks in advance!
Remove width: 100
<Card
style={{
backgroundColor: "#eee",
float: isSender ? "right" : "left",
padding: 16,
minHeight: 40,
width: 100,
display: "flex",
alignItems: "center",
textAlign: "center"
}}
>
{content}
</Card>
should be
<Card
style={{
backgroundColor: "#eee",
float: isSender ? "right" : "left",
padding: 16,
minHeight: 40,
display: "flex",
alignItems: "center",
textAlign: "center"
}}
>
{content}
</Card>
because the width: 100 gives it a width of 100px.
To make the content to stretch full width, you can use width: "fit-content" and assign your width value of 100 to minWidth: 100.
So your card will look like,
<Card
style={{
backgroundColor: "#eee",
float: isSender ? "right" : "left",
padding: 16,
diaplay: "block",
minHeight: 40,
width: "fit-content",
minWidth: 100,
display: "flex",
alignItems: "center",
textAlign: "center"
}}
>
To make the content left align inside card, you can assign style to Grid like,
<Grid style={{ paddingBottom: 8, textAlign: "left" }} item xs={12}>
<span>{name}</span>
</Grid>
Forked sandbox