how to pass props to higher order component
just pass your props as normal to the WrappedComponent
//App Component / root component
import React from 'react';
import WrappedComponent from "./WrappedComponent";
export default function App() {
return (
<div>
<WrappedComponent name="CreatorOfCode" />
</div>
)
}
//******************************************************************************
// WrappedComponent
import React from 'react';
function WrappedComponent({}) {
return (
<div>
<h1>Hello World</h1>
</div>
)
}
export default ComponentEnhancer(WrappedComponent)
//*****************************************************************************
// higher order component called ComponentEnhancer
import React from 'react';
import PropTypes from 'prop-types';
export default (WrappedComponent) => {
const hocComponent = ({ ...props }) => {
console.log(props.name)//CreatorOfCode
return <WrappedComponent {...props}
}/>
hocComponent.propTypes = {};
return hocComponent;
}
Are there any code examples left?
New code examples in category C
-
C 2022-03-27 22:30:45 Problem Statement Print the following output: \ Input Format IN Output Format \
-
-
-
-
-
-
-