Getting Minified React error #143 on antd dropdown - javascript

I am getting Minified error #143 when using antd dropdown with Space. I check and found this error code meaning as React.Children.only expected to receive a single React element child. But I have only one child in space component. below is the error:
Uncaught Error: Minified React error #143; visit https://reactjs.org/docs/error-decoder.html?invariant=143 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
at Object.only (react.production.min.js:20:474)
at overlay (dropdown.js:139:34)
at Dropdown.js:58:24
at V (Dropdown.js:85:26)
at d.getComponent (index.js:341:43)
at d.value (index.js:777:19)
at La (react-dom.production.min.js:182:192)
at Da (react-dom.production.min.js:181:224)
at mc (react-dom.production.min.js:263:490)
at lu (react-dom.production.min.js:246:265)
Below is my react code:
return (
<><Space>
<div className='dropdown-content'>
<div className='label'>{titles[0]}</div>
<div>
<Dropdown
menu={menu(clinicalStudies, clinicalStudiesOnClick)}
trigger={["click"]}
>
<Button>
<Space>
select data
<DownOutlined className="primary" />
</Space>
</Button>
</Dropdown>
</div>
</div>
<div className='dropdown-content'>
<div className='label'>{titles[1]}</div>
<div>
<Dropdown
menu={menu(clinicalStudiesDataSet, datasetObservationOnClick)}
disabled={disableDropdown}
trigger={["click"]}
>
<Button>
<Space>
select data2
<DownOutlined className="primary"/>
</Space>
</Button>
</Dropdown>
</div>
</div>
</Space></>
Can anyone confirm what I am doing wrong here?

You are displaying two children inside the Space component. You almost got it.
return (
<Space>
<React.Fragment>
<div className='dropdown-content'>
<div className='label'>{titles[0]}</div>
<div>
<Dropdown
menu={menu(clinicalStudies, clinicalStudiesOnClick)}
trigger={["click"]}
>
<Button>
<Space>
select data
<DownOutlined className="primary" />
</Space>
</Button>
</Dropdown>
</div>
</div>
<div className='dropdown-content'>
<div className='label'>{titles[1]}</div>
<div>
<Dropdown
menu={menu(clinicalStudiesDataSet, datasetObservationOnClick)}
disabled={disableDropdown}
trigger={["click"]}
>
<Button>
<Space>
select data2
<DownOutlined className="primary" />
</Space>
</Button>
</Dropdown>
</div>
</div>
</React.Fragment>
</Space>
);

Related

Map function not displaying data properly

I use to Map method to display data form API but data is not aligned properly I apply reverse method to reverse data but reverse is not working properly
I create this function to display data:
const blogCategory = (item) => {
return(
<>
<div>
<div key={item.title} className="mx-3" style=
{{display:"flex",justifyContent:"center",height:"750px"}}>
<div className="blog bolg-post">
<div className="img-hover-zoom">
<Link href={`/blogs/${item.title.replace(urlR,"-").replace('?',"")}`}>
<img style={{cursor:"pointer"}} className="blog-box" src={item.urlToImage}/>
</Link>
</div>
<div className="d-flex flex-column">
<span className="name-blog">{item.category}</span>
<Link href={`/blogs/${item.title.replace(urlR,"-").replace('?',"")}`}>
<span style={{cursor: "pointer",textAlign:"justify",maxHeight:"62px",overflow:"hidden"}} className="heading-blog">{item.title}</span>
</Link>
<div style={{height:"100px",overflow:"hidden",textAlign:"justify"}} className="para-blog">{item.meta_description}</div>
{/* <div style={{height:"100px",overflow:"hidden",textAlign:"justify"}} className="para-blog" dangerouslySetInnerHTML={{ __html: item.content }}/> */}
<Link href={`/blogs/${item.title.replace(urlR,"-").replace('?',"")}`}>
<button className="btn-blog mt-3">{item.readingTime} min read</button>
</Link>
</div>
</div>
</div>
</div>
</>
)
}
I use Map method to display data by using this function as show in below:
<div className='d-flex col-12' style={{width:"1000px"}}>
<div style={{height:"650px",overflow:'hidden'}}>
{cat.filter(categ=>categ.category === 'technology').reverse().map(
blogCategory
)}
</div>
<div className="products">
{cat.filter(categ=>categ.category === 'technology').map(
productCategory
)}
</div>
</div>
Data is displaying properly but does not align like [0,1,2,3,4,5,6,7,8,9] it displays like [9,8,7,6,5,4,3,2,1,0] .

How can I focus the cursor to the next TextInput in a React Draggable Menu?

I have a draggable context menu with three rows and three TextInputs. Right now I have two bugs:
When "tab" is hit, the menu closes
Even when I stop "tab" from closing (I return in the handleClose), "tab" doesn't focus the cursor to the next TextInput.
I've tried adding tabIndex={0} to the three divs that surround each TextInput, but this didn't help with the tab issue.
Does anyone know what might be wrong here?
Below is the simplified code (variables such as startNumber, endNumber, comments are state variables).
const handleClose = (event?, reason?) => {
if (reason && reason === 'tabKeyDown') {
// Prevent the 'Tab' key from closing the window
return;
}
setShouldOpen(false);
otherStuffHere();
}
...
return (
<Draggable cancel="textarea">
<Menu
open={shouldOpen}
onClose={handleClose}
anchorReference="anchorPosition"
anchorPosition={{ top: y, left: x }}
className={classes.myContextMenu}
>
<div className={classes.row}>
<div className={classes.title}>
<Text>First number</Text>
</div>
<div className={classes.text} tabIndex={0}>
<TextInput
fullWidth
value={startNumber}
disabled={readOnly}
onChange={handleStartNumberChange}
/>
</div>
</div>
<div className={classes.row}>
<div className={classes.title}>
<Text>End number</Text>
</div>
<div className={classes.text} tabIndex={0}>
<TextInput
fullWidth
value={endNumber}
disabled={readOnly}
onChange={handleEndNumberChange}
/>
</div>
</div>
<div className={classes.row}>
<div className={classes.title}>
<Text>Comments</Text>
</div>
<div className={classes.text} tabIndex={0}>
<TextInput
fullWidth
value={comments}
disabled={readOnly}
onChange={handleCommentsChange}
/>
</div>
</div>
</Menu>
</Draggable>
)
I wasn't able to find a solution while still using the Menu component, but I used the Popover component instead.
Using the same CSS and same everything else, I got the tab to work as intended without changing the UI.

Getting this React.Children.only expected to receive a single React element child error for using <Link> in Next.js

I have below code. It's giving me error
Error: React.Children.only expected to receive a single React element child.
As i try to resolve it,its allowing me only one header under link.
<div className="container">
{cards.map(card => (
<div className="grid-item">
<Link href={'/ninjas/' + card.id} key={card.id}>
<h1>{ card.reporter }</h1>
<h3>{ card.title }</h3>
</Link>
</div>
))}
</div>
Please first of all read this: Why React.Children.only?
The issue can be fixed by doing this:
<div className="container">
{cards.map(card => (
<div className="grid-item">
<Link href={'/ninjas/' + card.id} key={card.id}>
<div>
<h1>{ card.reporter }</h1>
<h3>{ card.title }</h3>
</div>
</Link>
</div>
))}
</div>
In general, Link is a component, and as you can read in the docs of react docs or the link I provided, children should always be nested in one element. The same is true for what every React component should return.

How can I style map items to display in justify-content-around?

I'm looping through products by mapping them and the justify-content-around doesn't apply to them and I'm using bootstrap 4 in my react project .
Here is my code :
{props.products.map((item) => {
if (item.inCart) {
return (
<div className="d-flex justify-content-around w-100" key={item.id}>
<button>remove</button>
<div>
<h5>{item.name}</h5>
</div>
<div>
<h5>{item.price.toLocaleString('ar-EG')}</h5>
</div>
<div>
<h5>{item.number.toLocaleString('ar-EG')}</h5>
</div>
<div>
<h5>{(item.number * item.price).toLocaleString('ar-EG')}</h5>
</div>
<hr />
</div>
);
}
The display flex class applies but not the other one and they all have no margin between them .
How can I make them to display justify-content-around ??
Your problem come from the <hr> tag. I just discovered it by doing this repro on stackblitz. Remove it and it works.
If you readlly need it then just receate it:
const Divider = () => {
return <div className="border-bottom" />;
};
This one is bootstrap dependant but of course you can make it generic easily. You just need to apply it outside of your flex container :
<>
<div className='d-flex justify-content-around' key={item.id}>
<button>remove</button>
<h5>{item.name}</h5>
<h5>{item.name}</h5>
<h5>{item.name}</h5>
</div>
<Divider />
</>;

How can I render multiple elements after an inline condition using short circuit evaluation?

Using a short circuit evaluation in a functional component (with hooks):
const Filters = () => {
const [options, setOptions] = useState([]);
return (
<div className="row">
{options.length > 0 && (
<div className="col-md-3">
<CustomComponent />
</div>
)}
</div>
)
}
Is there a way to render multiple elements right after an inline if condition?
{options.length > 0 && (
<div className="col-md-3">
<CustomComponent />
</div>
<div className="col-md-3">
<SecondComponent />
</div>
)}
Code above doesn't work, react throws an error. options array is filled after a promise resolved from useEffect(). That's why i've added this check, to display elements only when a promise resolved.
UPDATED CODE for #wentjun:
return (
{options.length > 0 && (
<div className="row">
<div className="col-md-3">
<CustomComponent />
</div>
</div>
)}
)
This throws a syntax error.
I think the error is due to returning two react elements. Try wrapping then in fragment
{options.length > 0 && (
<>
<div className="col-md-3">
<CustomComponent />
</div>
<div className="col-md-3">
<SecondComponent />
</div>
</>
)}
You should wrap the elements in React Fragments. The reason being, React elements are rendered via a single root node element. Wrapping it in Fragments will allow you to group the elements without adding additional nodes.
return (
<div className="row">
{options.length > 0 && (
<>
<div className="col-md-3">
<CustomComponent />
</div>
<div className="col-md-3">
<SecondComponent />
</div>
</>
)}
</div>
)

Categories