react hoc functional component code example
Example 1: functional component react
function Welcome(props) {
return Hello, {props.name}
;
}
function App() {
return (
);
}
ReactDOM.render(
,
document.getElementById('root')
);
Example 2: 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 (
)
}
//******************************************************************************
// WrappedComponent
import React from 'react';
function WrappedComponent({}) {
return (
Hello World
)
}
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
hocComponent.propTypes = {};
return hocComponent;
}
Example 3: functional components react
function Comment(props) {
return (
{props.author.name}
{props.text}
{formatDate(props.date)}
);
}
Example 4: high level components react
import React, { useState } from 'react';
class WelcomeName extends React.Component {
render() {
return Hello, {this.props.name}
}
}
function HighHookWelcome() {
const [name ,setName] = useState("Default Name Here");
return(
);
}
// Note: Didn't need to use state for a static value.
// Minimal example, just to illustrate passing state/props.
// setName is a function to change the name value.