I am trying to understand the below React HOC;
const withSecretToLife = (WrappedComponent) => {
class HOC extends React.Component {
render() {
return (
<WrappedComponent
{...this.props}
secretToLife={42}
/>
);
}
}
return HOC;
};
export default withSecretToLife;
And below is another component;
import withSecretToLife from 'components/withSecretToLife';
const DisplayTheSecret = props => (
<div>
The secret to life is {props.secretToLife}.
</div>
);
const WrappedComponent = withSecretToLife(DisplayTheSecret);
export default WrappedComponent;
Specifically I am trying to understand
- If it "DisplayTheSecret" that gets access to the prop "secretToLife" Or the WrappedComponent ? What is the correct way to look at this HOC ?
- The line "const WrappedComponent = withSecretToLife(DisplayTheSecret);" is after the line "const DisplayTheSecret = props => ()". Is that a standard pattern and does it matter?
- Related to above question, how are we able to do "props.secretToLife" before we even have the
declaration/definition . for the WrappedComponent i.e. const
WrappedComponent = withSecretToLife(DisplayTheSecret);
- What is actually consumed/used in the above case say in App.js i.e
<WrappedComponent />
OR <DisplayTheSecret />
OR either of them can be consumed ?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…