Parsing error in React class Component while using condition - reactjs

I get this Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>? error while everything in my Component is set up correctly. I have double-cheked everything and there seems to be nothing wrong. Collapse component is highlighted in error message. What have i done wrong or what could cause error here?
Code part with error:
return (
<div>
{
isAuthenticatedAndIsCoach ?
<h2>Lisa</h2>
<Collapse defaultActiveKey={['0']} onChange={callback} style={{ marginBottom: '10px', borderRadius: '10px'}}>
<Panel header="Valige treeningu asukoht kaardilt" key="1">
<Map />
</Panel>
</Collapse>
<CustomForm
requestType="post"
trainingID={null}
btnText="Loo treening"/>
: <span>Teil puudub vastav luba postitamiseks</span>
}
</div>
)

You should wrap JSX returned by ternary condition with React.Fragment (or <>) as React expects a single element each time a render function is executed.
https://en.reactjs.org/docs/fragments.html
return (
<div>
{isAuthenticatedAndIsCoach ? (
<>
<h2>Lisa</h2>
<Collapse
defaultActiveKey={["0"]}
onChange={callback}
style={{ marginBottom: "10px", borderRadius: "10px" }}
>
<Panel header="Valige treeningu asukoht kaardilt" key="1">
<Map />
</Panel>
</Collapse>
<CustomForm
requestType="post"
trainingID={null}
btnText="Loo treening"
/>
</>
) : (
<span>Teil puudub vastav luba postitamiseks</span>
)}
</div>
);

Related

Warning: Received `true` for a non-boolean attribute `item`

This error shows in my dev tools when I load my app. I'm not sure how to go about changing it to fix the problem. I suspect it is a reason why when I deploy it to heroku it looks different that when I view it locally so I'm hoping if I fix this it will fix my problem with heroku as well.
Here is my code:
return (
<Container className="container" maxWidth="xl">
<CssBaseline />
{/* <Box sx={{ marginTop: 10, padding: 7, bgcolor: "#eeeeee", borderRadius: "0.3rem" }}> */}
<Grid className="homeContainer" container spacing={1}>
<Grid className="topGrid">
<Grid className="imageContainer" item xs={4} sx={{ }}>
<img className="cctLogoLarge" src={cctlogo1} alt="outline of horse with parliament buildings in background"></img>
</Grid>
<Grid className="introContainer" item xs={12}>
<p>Welcome to Cap City Trotters! The CCT was created in 2019 by KMAC, Lynch, Bruster, and Damarts. Our Routes include several loops in both Ottawa and Gatineau.
We are always looking for new members so if you want to join you can take a look at some of our main routes and signup if you like what you see!</p>
{/* Test Connect to Strava */}
</Grid>
</Grid>
<Grid >
<Grid className="postList-grid">
{/* {loggedIn && (
<div className="col-12 mb-3">
<PostForm />
</div>
)} */}
<Grid item xs={6} sx={{ bgcolor: ""}} className="recentPosts">
{loading ? (
<div>Loading...</div>
) : (
<PostList posts={posts}/>
)}
</Grid>
{loggedIn && userData ? (
<Grid>
{/* <FriendList
username={userData.me.username}
friendCount={userData.me.friendCount}
friends={userData.me.friends}
/> */}
</Grid>
) : null}
</Grid>
</Grid>
{/* </Box> */}
</Grid>
</Container>
)
I've looked at similar questions and answers but can't figure out how to solve this problem specifically.
Thanks in advance!
Whenever you add a prop without a value to React component, it evaluates to true:
<MyComponent boo />
Here, MyComponent receives a boo = true property. That's what you have in your code with item, while it shouldn't be a boolean value. Just pass some value to item:
<Grid item={somethingElse}>

I m trying to use a "if" statement but it doesnt run

I have this components
and i have this code as the if sentence.
I have the same method in other project that works, can anyone help?
it should only render the first one as addnewchat hte others ones should be avatars, if i take of the "!" only renders the avatars.
You can change the syntax of if and else. Instead of using ternary operator directly after return keyword, use a parent tag, and then wrap your whole component inside it. Like
return (
<>
{addNewChat ? (
<div>if logic here</div>
) : (
<div>else logic here</div>
)}
</>
)
calling component:
<div className="sidebar__chats">
<SidebarChat addNewChat />
<SidebarChat />
<SidebarChat />
<SidebarChat />
<SidebarChat />
</div>
code:
return (
<>
{addNewChat ? (
<div className="sidebarChat">
<Avatar
alt="João Espinheira"
src={`https://avatars.dicebear.com/api/pixel-art/${seed}.svg`}
sx={{ width: 38, height: 38 }}
/>
<div className="sidebarChat__info">
<h2>room name</h2>
<p>last message...</p>
</div>
</div>
) : (
<div onClick={createChat} className="sidebarChat">
<h2>add new chat</h2>
</div>
)}
</>
);
}
i think the code should work, no?
function SidebarChat({ id, name, addNewChat }) like this works fine!
previously i had left a part the {} so that was the thing it was missing

Conditional rendering of parent component in React

I need to return either Porover or Card depending on the condition with the same children content. This can be done by taking the children content into a separate component and returning it like this:
true ? <Popover><ChildrenContent props={props}/></Popover>
: <Card><ChildrenContent props = {props}/></Card>
But it seems to me that this is not the best solution in this case
<Popover dataCy="complexValidation">
<Section marginBottom={3}>
</Section>
<Flex flexDirection="column" gap={3}>
{validations.map((validation) => (
<Flex.Item key={validation.text}>
<Flex alignItems="center">
<Icon name="check_circle" size="large" color={validation.isSuccess ? 'positive' : 'negative'} />
<Flex.Item>
<Typography variant="bodyText2" component="span">
{validation.text}
</Typography>
</Flex.Item>
</Flex>
</Flex.Item>
))}
</Flex>
</Popover>

admin-on-rest toggle menu (show / hide)

I'm using admin-on-rest in my react application as admin interface.
My problem is the menu, I'm add a DashboardMenuItem to toggle (show/hide) the menu. But I have no idea what the onClick function should look like and I find no example of this in the documentary or somewhere else.
Can anyone help me by giving an example for this?
My Code:
const Menu = ({ hasDashboard, onMenuTap, resources, translate, logout }) => (
<div style={styles.main}>
<WithPermission value='ROLE_SA'>
{hasDashboard && <DashboardMenuItem onClick={onMenuTap} />}
</WithPermission>
{resources
.filter(r => r.list)
.map(resource => (
<MenuItemLink
key={resource.name}
to={`/${resource.name}`}
primaryText={translatedResourceName(resource, translate)}
leftIcon={<resource.icon />}
onClick={onMenuTap}
/>
))}
{/* <MenuItemLink primaryText='Reports' key='reports' to={`/reports`} leftIcon={<UserIcon />} onClick={onMenuTap} /> */}
<WithPermission value='ROLE_SA'>
<SelectField floatingLabelText='Language for Datasets' onChange={LocaleSwitcher}>
<MenuItem value={'de'} primaryText='DE' />
<MenuItem value={'en'} primaryText='EN' />
</SelectField>
</WithPermission>
{logout}
<img src={Logo} style={{maxWidth: '100%', margin: '0 auto'}} />
</div>
)

Filtering with map in reactjs

I'm trying to do a filter in my app, where the content of the table to filter is defined in the reducer and comes as a state to the component. To do this i'm adding a filter to the mapping of the array.
{tilesData
.map((tile, i) => (
<GridListTile key={i}>
<img src={projectA} style={style.img} alt={tile.title} />
<GridListTileBar
title={<span>{tile.title}</span>}
subtitle={<span>{tile.address}, {tile.state} </span>}
actionIcon={
<Link to = 'project'>
<IconButton onClick={goToProject(i)} color='contrast'>
<VisibilityIcon />
</IconButton>
</Link>
}
/>
</GridListTile>
))
.filter(tilesData.title === project)
}
After doing this, it throws the following error:
TypeError: false is not a function

Resources