score:1
This answer will not solve your problem but maybe gives a hint why this is not possible. At first I was surprised why your code does not work, even though I'm not an experienced React developer it seems ok map this.props.children
through with React.Children.map
and return the desired Component with your HOC. But it did not work.
I tried to debug it a little bit and did some search. I've learned props.children
actually contains the elements itself not the instances of components. Even, React.Children.map
does not have any effect on this.
Here is a working snippet proves that your problem is not related with the HOC. I've used an array of components instead of mapping through props.children
.
class Line1 extends React.Component {
componentDidMount() {
setTimeout(this.props.onAnimationEnd, 1000);
}
render() {
return <div>Line 1</div>;
}
}
class Line2 extends React.Component {
componentDidMount() {
setTimeout(this.props.onAnimationEnd, 1000);
}
render() {
return <div>Line 2</div>;
}
}
class Line3 extends React.Component {
componentDidMount() {
setTimeout(this.props.onAnimationEnd, 1000);
}
render() {
return <div>Line 3</div>;
}
}
const withNotification = handler => Component => props => (
<Component onAnimationEnd={handler} {...props} />
);
class Sequence extends React.Component {
constructor(props) {
super(props);
this.state = {
pointer: 0
};
this.notifyAnimationEnd = this.notifyAnimationEnd.bind(this);
this.Arr = [ Line1, Line2, Line3 ];
this.Children = this.Arr.map(Child =>
withNotification(this.notifyAnimationEnd)(Child)
);
}
notifyAnimationEnd() {
this.next();
}
next() {
// Clearly, render the next element only if there is, a next element
if (this.state.pointer >= this.Arr.length - 1) {
return;
}
this.setState({ pointer: this.state.pointer + 1 });
}
render() {
return (
<div>
{this.Children.map((Child, i) => {
if (i <= this.state.pointer) return <Child />;
return <div>nope</div>;
})}
</div>
);
}
}
ReactDOM.render(
<Sequence />,
document.getElementById("app")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>
score:0
You are returning <Child />
instead of Child
in Sequence.js render method. Here is my edited copy - codesandbox
Source: stackoverflow.com
Related Query
- How can I wrap the Children of a Parent component in a HOC and render them in React?
- How can I render Parent component again after update from the child component
- How can I shuffle an array of objects on page render and map over the shuffled array without console warning of "two children with the same key"?
- I'm using react-slick, how can I export a React Component as a single element but remove the parent div on render
- How can I manage a list from Parent component in React and render it?
- How to render the parent component in the child component using react and typescript?
- How can I set the parent component state based on children component checkbox value?
- How can i render a route component in the parent component from a child component
- how can i loop through the array inside of an object. and render it in the react component
- How can I set 2 values for one variable, and the component will render with one value, depending of some prop input
- how can i reuse parent component and give children props in react?
- How can i pass different props to more than one children component from the parent one in React?
- How do I wrap a React component that returns multiple table rows and avoid the "<tr> cannot appear as a child of <div>" error?
- How can I animate a react.js component onclick and detect the end of the animation
- ReactJS how to render a component only when scroll down and reach it on the page?
- react - how do I get the size of a component child elements and reposition them
- How can I pass CSS classes to React children and ensure they will override the child’s local class?
- How do I manage state on a React component that can have state changed from the parent or from events upon it?
- How do we return a Promise from a store.dispatch in Redux - saga so that we can wait for the resolve and then render in SSR?
- How to render only the concerned component with a custom hook and useContext
- how can I make a scrollable component that scrolls to the latest children components automatically when I append a child component?
- How can I not pass children as props. Instead, nest children between the opening and closing tags on React?
- How can I use Promise.all on an array of objects and then reassign them to the relevant key in a new object?
- How can I use useEffect in React to run some code only when the component first mounts, and some other code whenever an event occurs (repeatedly)?
- How to make an http call in parent component to setState and then send the state to all child components?
- How can I change a child's state from the parent component
- How I can loop through the similar objects in state and push them into object?
- How can parent component reset many children in React
- How can I get a component to render as a parent of protected routes in react router dom?
- How can I redirect to another component in react and pass the state that I set in the previous component?
More Query from same tag
- how to split the result in array React/Axios
- How to prevent user from going back in react-router-dom?
- why to import React
- Value is not getting dispatched
- Why state returned from my reducer is an empty object??(React-Redux)
- Embed a var in a backgroundImage url in a reactJS
- How to manage array saved in reactjs state
- Firebase V9 Online Status
- React Props not being returned, but being returned in console
- controlled radio button warning handling it's value with formik field
- Strange behavior after React state update with new entry
- Passing prop values to custom styled component
- How to style components properly in React
- How to make payments with stripe in a django and react app?
- How to fetch react-cookies if CORS is blocking it
- Initialize React class/component on existing Website with attributes
- React fetch api (without hooks)
- How to do word-wrap for data using react-table?
- How to get the marquee effect with list view in react native?
- react child state doesn't update with props from father (and deeper in the branch)
- Switch over to Create React App on Existing Project?
- Convert integer to time format
- browserify - exclude code blocks?
- getting started with reactjs component is not working
- pass multiple local storage videos to react js popup video player
- Add to Homescreen React PWA
- Get a value from a <i> tag with hooks in react
- XML sitemap for react app
- How to convert ES6 JSON object to dictionary?
- Animating a custom carousel with React.js