score:0
Accepted answer
You are not using find properly. If you return anything truthy in its callback, it will return the current element.
this.getSelectedNode(element.nodes)
will eventually return the right element, but it will be treated as a truthy value, and it will mean that no matter what you do, the first element will be returned if any of it's descendants it's a match.
What you want to do is something like this:
getSelectedNode(nodes) {
// Use a for loop instead of forEach to avoid nested functions
// Otherwise "return" will not work properly
for (const element of nodes) {
// Check if this element is the seleted one, return it if it is
if(element.name === this.state.selectedItem) {
return element
}
// If it's not - recursively check its descendants, if it has any
if(element.nodes){
const found = this.getSelectedNode(element.nodes)
if(found){
return found
}
}
}
// didn't find anything, return null
return null
}
Source: stackoverflow.com
Related Query
- Recursion returns parent but not child
- React redux - parent state changing, but child components not re-rendering
- React Router returns child components and not parent component
- Material-ui theme parent component but not child component of same type
- Child Not Re-rendering on List Item Deletion in Parent but Adding to the List Item Triggering Re-render
- ReactJS - Child components DO NOT render when imported component used, but DO when explicitly marked up in parent element
- State from custom hook using use reducer updates in parent but not child
- passing data from parent class to child class through props, but props not returning anything in child class
- React: Child component is passing it's state up to parent, but once parent has it, the parent is not updating its state
- Piece of state changes in parent component but not in child components React
- Custom Hook is updating either Parent or Child component but not both
- Why parent state is not updating when I change a value on a child but it is working the other way around
- React - State is defined in one parent function, but not in the other parent function being called from the child
- React parent component state updates but child does not re-render
- React Child Component Not Updating After Parent State Change
- react routing is able to handle different url path but tomcat returns 404 not available resources
- ReactJS: "this.props" is not a function when child component calls parent
- Why is child component not receiving updated value from parent in React Native?
- TextField default value from parent not rendering on child
- React Child with useEffect() not updating on Parent State Change
- React JS Fetch Returns Empty Response but Postman Does Not
- React Router parent ".active" class not active when child router loaded
- Is parent component re-rendered when the child component returns with something new?
- React child component not re-rendering on parent state change
- Typescript returns error when a function type is FunctionalComponent, but not for arrow function
- React hooks: Parent component not updating when dispatch is called in a child component
- React: Parent component refs not accessible to newly mounted child component?
- React child component not rendering props from parent value
- Typescript+React+Redux+reactrouter:"property does not exist on type IntrinsicAttributes & IntrinsicClassAttributes" passing props from parent to child
- MapStateToProps and MapDispachToProps in parent and child components not working
More Query from same tag
- Button onClick triggering when modal opens
- Make second api call after first api call returns data
- MUI: How to delete selected rows in DataGrid programmatically?
- React hooks with async fetch
- Why react-router Route attr render remount not rerender when component update?
- Positioning a NavBar in React
- Using <audio> tags in React app, playing chosen file onClick
- Login status in React
- How to list all the names and download Urls from Firebase using React JS?
- React leaflet map location glitches when not in App.js
- React Router with React 16.6 Suspense "Invalid prop `component` of type `object` supplied to `Route`, expected `function`."
- Position and centering of the circular shape inside the list cell in CSS
- Uncaught TypeError: Cannot read property 'append' of null in typescript
- Can't pass a function to props
- OnChange event for elements in html set with DangerouslySetInnerHTML in ReactJS
- Testing custom hook with SetTimeout and useEffect with Jest
- Cannot read property 'map' of undefined - react , redux
- React pass data from front-end to back-end node
- How to detach mock function from the react component's prototype so that will not affect other test?
- How to handle onError for img tag in react?
- React TransitionGroup lifecycle methods not being called when component is loaded
- State being set with delayed selected value when onChange is called
- React Hook testing with renderHook
- react hooks cannot read property map of undefined
- How can I apply default styles for react-select when defining a custom Option Component?
- react-joyride - How to close tutorial when clicking on the cross?
- Save state of multiple Dynamic Components on ReactJS
- Is there a way to access a function returned by a React component through a parent HTMLDivElement?
- Error : Object are not valid as a react child
- React - How can I wrap and element depending on a boolean parameter?