Comments (6)
Hi!
Is it possible to share your code (at least the code of the whole MDBDropdown component and structure. The sample repository, or more code would be great)? I see the problem is with React.cloneElement
, there are no elements to clone, so I think for now the problem is there.
Keep coding!
from mdb-react-ui-kit.
Hi @krzysiu-w
Here I am sharing a zip of my sample code.
I have searched for the reason and found that when any null argument pass into MDBDropdownItem this issue occurs
<MDBDropdownItem onClick={() => setIsShow(true)}>
<span>Simple search</span>
{isShow && (<i className='fas fa-check ml-2'></i>)} // The issue occurs here
</MDBDropdownItem>
<MDBDropdownItem onClick={() => setIsShow(false)}>
<span>Extended search</span>
{!isShow && (<i className='fas fa-check ml-2'></i>)} // The issue occurs here
</MDBDropdownItem>
Thanks
from mdb-react-ui-kit.
Hi!
The problem is that your MDBDropdownItem
component doesn't have any children if it is rendered conditionally. So when your isShow
is true
the second item is empty and if it has false
- the first one, so the React.cloneChild
can't map the children. To avoid that you can just render conditionally the whole item as I show below - it works perfectly :)
<MDBDropdown>
<MDBDropdownToggle className='btn btn-white waves-effect px-3 mr-2 ml-0 mt-0 mb-3 p-2 icon-btn'>
<i className='fa fa-search mr-2 text-primary'></i>
<span>Search options</span>
</MDBDropdownToggle>
<MDBDropdownMenu>
<MDBDropdownItem onClick={() => console.log('click reload')}>
<i className='fas fa-sync-alt mr-2'></i>
<span>Refresh</span>
</MDBDropdownItem>
{isShow && (
<MDBDropdownItem onClick={() => setIsShow(false)}>
<span>Simple search</span>
<i className='fas fa-check ml-2'></i>
</MDBDropdownItem>
)}
{!isShow && (
<MDBDropdownItem onClick={() => setIsShow(true)}>
<span>Extended search</span>
<i className='fas fa-check ml-2'></i>
</MDBDropdownItem>
)}
<MDBDropdownItem
onClick={() => console.log('click column chooser')}
>
<span>Select columns</span>
</MDBDropdownItem>
</MDBDropdownMenu>
</MDBDropdown>
Keep coding!
from mdb-react-ui-kit.
Hi @krzysiu-w
Thanks for the solution.
I have updated my code as you suggest, but my requirement is only toggle the icon, not the whole item.
And this is working fine with previous version (mdb4) and it's better to keep item and toggle only the icon on specific condition.
So kindly look into it.
Thanks
from mdb-react-ui-kit.
Hi!
That's a logical problem. When React checks this statement {isShow && (<i className='fas fa-check ml-2'></i>)}
it expects an additional node. So when the isShow
state is set to false - Array.cloneChild
tries to render a child that doesn't exist. To avoid that you also can use a ternary operator instead of logical AND one and return an, i.e., empty span like below:
<MDBDropdownItem onClick={() => setIsShow(false)}>
<span>Simple search</span>
{isShow ? (
<i className='fas fa-check ml-2'></i>
) : (
<span></span>
)}
</MDBDropdownItem>
Keep coding!
from mdb-react-ui-kit.
Thanks @krzysiu-w
from mdb-react-ui-kit.
Related Issues (20)
- iOS/Android Deployment Error HOT 1
- Broken MDBCollapse component in version 2.1.0 HOT 1
- Dropdown menu position issue HOT 1
- TimePicker can not get the value I set HOT 1
- ReferenceError: document is not defined HOT 1
- Replace 'transition-property' with 'transition' HOT 2
- Question: mdbreact vs mdb-react-ui-kit - which package is better? HOT 4
- cli is looking for react v17 HOT 1
- Runtime error HOT 1
- MDBModal keeps display:block after 2nd time closed HOT 7
- Module parse failed error HOT 2
- MDBCollapse keep height after animation ends HOT 2
- MDBCollapse does not have onHide and onShow events HOT 3
- uncaught TypeError: Failed to execute 'getComputedStyle' occurs and keeps racking up but doesnt crash anything HOT 7
- Failed to execute 'getComputedStyle' on 'Window' HOT 1
- Using MDBootstrap with next.js App Router HOT 1
- Error when installed version of chart.js is > 4.3.3
- Uncaught TypeError when setting leaveHiddenModal to false on MDBModal HOT 1
- Community & Migration HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mdb-react-ui-kit.