score:1
Accepted answer
As the error says, you are trying to use an object as a react component (childProp.title
, and childProp.content
). It seems you want to use their children as "content".
import { useMemo } from "react";
const useChildProps = (children, whitelist) => {
return useMemo(() =>
[].concat(children).reduce(
(childProps, child) => {
if (whitelist && !whitelist.includes(child.type)) {
throw Error(`element <${child.type}> is not supported`);
}
childProps[child.type] = child.props;
return childProps;
},
{}) // better make this an object, not an array that you were monkey-patching properties into
);
};
export default useChildProps;
import useChildProps from "./useChildProps";
const ModalFromTheFuture = ({ children }) => {
const childProps = useChildProps(children, [
"title",
"contents",
"actionButton",
"cancelButton"
]);
console.log(childProps);
delete childProps.actionButton.children;
delete childProps.cancelButton.children;
return (
<div>
<header>{childProps.title && <h1> {childProps.title.children}</h1>}</header>
<section>
<p>{childProps.contents && childProps.contents.children}</p>
</section>
<footer>
{childProps.actionButton && <button {...childProps.actionButton} />}
{childProps.dismissButton && <button {...childProps.cancelButton} />}
</footer>
</div>
);
};
Source: stackoverflow.com
Related Query
- React native - objects are not valid as a React child (found: object with keys {$$typeof, type, key, ref, props, _owner, _store})
- Objects are not valid as a React child (found: object with keys {})
- Objects are not valid as a React child (found: object with keys {children}): ReactJS
- React.js Error: " Objects are not valid as a React child (found: object with keys {})."
- Objects are not valid as a React child (found: object with keys {username}). If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child (found: object with keys {id, name})
- Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child (found: object with keys {$$typeof, type, compare, WrappedComponent, displayName})
- Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead
- Error: Objects are not valid as a React child (found: object with keys {}). use an array instead
- Error: Objects are not valid as a React child (found: object with keys {})
- React with Router v5 Error: Objects are not valid as a React child (found: object with keys {children})
- Objects are not valid as a React child (found: object with keys {job}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {children})
- 'Error: Objects are not valid as a React child (found: object with keys {results, info})'
- Error: Objects are not valid as a React child (found: object with keys {zip})
- Error: Objects are not valid as a React child (found: object with keys {low, high})
- Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys {$$typeof, render})
- Failed to render a d3js object in Reactjs: Objects are not valid as a React child (found: object with keys {_groups, _parents})
- Objects are not valid as a React child (found: object with keys {totalItems}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {arr}). If you meant to render a collection of children, use an array instead
- Firebase Error: Objects are not valid as a React child (found: object with keys {seconds, nanoseconds})
- Error: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead. JS
- Objects are not valid as a React child (found: object with keys {weight}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {..}). .....use an array instead. in Select (created by Context.Consumer)
- I AM HAVING ERROR WHEN TRYING TO MAP A DATA IN REACT . Objects are not valid as a React child (found: object with keys {children}), How do i solve it?
- React , Error: Objects are not valid as a React child (found: object with keys {data})
- What is causing this error - Objects are not valid as a React child (found: object with keys {id, task})
- Error: Objects are not valid as a React child (found: object with keys {name}). If you meant to render a collection of children, use an array instead
- Objects are not valid as a React child (found: object with keys {children}). If you meant to render a collection of children, use an array instead
More Query from same tag
- Make ReactDOM.createPortal insert element as first child of target container (instead of last child)
- React Socket io send data to client to specific emit
- React router with multiple params
- Uncaught Error: [RouteWrapper] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>
- ReactJS state updates one step behind
- Server returns status 403 forbidden when i send request with bearer token
- React input elements all re-render when onChange is called on a single input, even with memo on the input and useCallback on the onChange
- Loading an External Script From AD Partner in ReactJS
- Chunked loading of assets for CRA SSR
- access the theme from outside material-ui component
- Apply CSS Module class name by directly accessing the DOM
- Hiding Sidebar of semantic ui react in desktop or tablet view
- chess.js: Move validation not working correctly
- How to change the border color when error Helper text appears on TextField?
- Can't get value of selected option
- React: Set a path as landing page
- How to set Loading when the button is clicked and show the result after timeout?
- How to access nested object in React js 0.13.3
- useDispatch() on onChange prop of input field in React form
- How can I avoid circular imports in routes file
- How to check if a div is overflowing in react functional component
- How to get absolute value from animated value in react native?
- How to handle error.touched and errors with an object in Formik
- importing in prop array values in React?
- How can I show an "email-id already exists" message in React.js?
- ReactJS change button on click
- Error saying Parsing Error: Unexpected token
- How do i delete an item from my shoping cart in javascript?
- React importing but not using in index.js
- ref.current is always undefined when using ref from forwardRef