score:3
This is an anti-pattern, as @hamms has pointed out. There are better ways to implement themes in React using plain old CSS.
Said that, here's a hack to a working example of your use case - https://codesandbox.io/s/ymqwyww22z.
Basically, here's what I have done:
Make
List
a class based component. It's not too much trouble to wrap a functional component into one.import React, { Component } from "react"; export default class List extends Component { render() { return ( <ul> <li>1</li> <li>2</li> </ul> ); } }
Implement
render
in the dynamic classRed<Component>
to first fetch the element-tree returned from the base Component's render and then edit it.import React from "react"; export default function makeRed(Component) { return class RedComponent extends Component { constructor(props) { super(props); RedComponent.displayName = `Red${Component.name}`; } render() { let componentElement = super.render(); let { children, ...rest } = componentElement.props; children = React.Children.map(children, child => { return React.cloneElement(child, { ...child.props, style: { backgroundColor: "red" } }); }); return React.cloneElement(componentElement, rest, ...children); } }; }
How's this different from the createElement
version of makeRed
?
As makeRed
returns a HOC, when you use it in your App
component, you don't assign props to it. Something like this...
function App() {
return <RedList />; // no props!
}
So inside the dynamic component function, where you use createElement
to create a new instance, component.props
don't carry any children. Since List
creates its own children, you need to grab those and modify them, instead of reading children from props
.
Source: stackoverflow.com
Related Query
- Access children of react element
- How to access a DOM element in React? What is the equilvalent of document.getElementById() in React
- access root element of react component
- TypeScript React - access video element by ref and call .play()
- React access DOM element instead of object thru refs
- How to access an input element of type checkbox and name using react testing library?
- Add children to React element
- Access all children component of a React component
- Cant access children in component used as element in Route (react-router-dom 6.3.0)
- How do I access a DOM element in a function where this.refs is unavailable in a React app?
- Ant Design Dropdown - Access props value inside children menus [ React ]
- Access and change the state array element in main React App from grandchild
- Event object from onClick event couldn't access one of the props on the element in react
- Type Children Element is missing properties with Nextjs + React Context with TypeScript
- How to improve React performance when change element position with a lots children element
- React children prop element used conditionally vs non-conditionally
- Access React DOM Element from external script
- Objects are not valid as a React child - having to access to two children
- How to I access DOM element using 'this' keyword in React
- How to access more than one element of Dom in react using React.createRef()
- I am trying to access element inside modal(mui modal) using ref in react but reference to element is null
- React HTML select element onChange function, trying to access 'event.target.value'
- how to access children props in React
- How do I access the children of the clicked element using REACT?
- React functional component - How to access children method from Parent
- How to access properties in child from children props in react
- Trying to access element in JSON using Google Books API using React
- How to pass dynamic value to div element and then access it - React ?
- React way of setting click handler event on element but not its children
- Not able to access array element in react
More Query from same tag
- React Error: Target container is not a DOM Element: got when loading a Modal Form
- Transform: translate Doesn't work in React / SASS
- Adding a button/icon to each row of a Semantic UI React dropdown
- React Typescript Custom Hooks: Property 'prop_name' does not exist on type '{}'
- React Leaflet Routing Machine: onClick to add Marker after all waypoints are removed fires only after second click
- Why does componentDidMount fires in a HOC where as componentDidUpdate does not fire?
- How to display certain elements of a list as sticky elements without adding any hierarchy in rendering?
- How to automatically copy the text of an input?
- I am trying to pass date object and other nested objects in query which is getting undefined in the search page
- React hook with constant input parameter - hook creator?
- Typescript not inferring correct type after type check inside a ternary operator
- getElementById returning null but getElementsByClassName works, why?
- React page doesn;t load when adding map
- Running function twice and saving the output for comparison
- Importing an image into an <src> tag in React.js when the image name is not known in advance
- How can I match data from an object and pass it as props to another component that displays dynamically?
- React props show up as undefined at first
- how do you use and change innerHTML in react?
- Checking if a component is unmounted using react hooks?
- getting 'NULL' error while I am testing 'document.getElementByID' line. I already tried attachTo approach but that is also not working
- `The 'async' modifier can only be used in TypeScript files.ts(8009)` error when using async in react.js
- Multiple loading in Redux
- why Can't change the state in the render method. What is the best place to change the state before calling render method
- React: Expected `onClick` listener to be a function, instead got a value of `object` type but only when I pass it to a child component
- how to render an element n number of times in react
- Reactjs - create factory, passing props into component
- How to accept a nullable Long in an API endpoint?
- unexpected token reactjs app on codesandbox
- React How to add on hover border for <area> tag
- How setState affects setInterval?