Loading...
Loading...
Compare original and translation side by side
stylestylefunction withStyles(Component) {
return props => {
const style = { padding: '0.2rem', margin: '1rem' }
return <Component style={style} {...props} />
}
}
const Button = () = <button>Click me!</button>
const Text = () => <p>Hello World!</p>
const StyledButton = withStyles(Button)
const StyledText = withStyles(Text)withStyles"Loading..."DogImageswithLoaderwithLoaderLoading…function withLoader(Element) {
return (props) => <Element />;
}withLoaderwithLoaderfunction withLoader(Element, url) {
return (props) => {};
}props => {}Loading…useEffectwithLoaderurlLoading...datadatanullDogImages.jsDogImageswithLoadingDogImagesexport default withLoader(
DogImages,
"https://dog.ceo/api/breed/labrador/images/random/6"
);withLoaderstylestylefunction withStyles(Component) {
return props => {
const style = { padding: '0.2rem', margin: '1rem' }
return <Component style={style} {...props} />
}
}
const Button = () = <button>Click me!</button>
const Text = () => <p>Hello World!</p>
const StyledButton = withStyles(Button)
const StyledText = withStyles(Text)withStylesDogImageswithLoaderwithLoaderfunction withLoader(Element) {
return (props) => <Element />;
}withLoaderwithLoaderfunction withLoader(Element, url) {
return (props) => {};
}props => {}useEffectwithLoaderurldatadatanullDogImages.jsDogImageswithLoadingDogImagesexport default withLoader(
DogImages,
"https://dog.ceo/api/breed/labrador/images/random/6"
);withLoaderHovering!DogImageshoveringDogImageswithHoverwithLoaderDogImageswithHoverwithLoaderHovering!hoveringtruefalseA well-known library used for composing HOCs is recompose. Since HOCs can largely be replaced by React Hooks, the recompose library is no longer maintained.
DogImageshoveringDogImageswithHoverwithLoaderDogImageswithHoverwithLoaderhoveringtruefalse一个用于组合HOC的知名库是recompose。由于HOC在很大程度上可以被React Hooks替代,recompose库已不再维护。
withHoveruseHovermouseOvermouseLeaverefmouseOvermouseLeaveuseEffecthoveringtruefalserefhoveringrefmouseOvermouseLeavehoveringHovering!DogImageswithHoveruseHover<withAuth>
<withLayout>
<withLogging>
<Component />
</withLogging>
</withLayout>
</withAuth>Note (React 18+): Modern React strongly prefers Hooks or other composition patterns to reuse logic with less nesting. Excessive layering of HOCs leads to "wrapper hell" with deeply nested wrappers, making debugging difficult and cluttering React DevTools. Libraries like Recompose are no longer maintained because Hooks can largely replace their functionality. For example, instead of aHOC, you can create awithHoverhook that returns whether an element is hovered and a ref to attach. This avoids an extra component layer and meshes well with React's upcoming features — the React Compiler can better analyze component code when logic isn't hidden in HOCs.useHover
useHoverwithHovermouseOvermouseLeaverefrefmouseOvermouseLeaveuseEffecthoveringtruefalserefhoveringrefmouseOvermouseLeavehoveringwithHoverDogImagesuseHover<withAuth>
<withLayout>
<withLogging>
<Component />
</withLogging>
</withLayout>
</withAuth>注意(React 18+): 现代React强烈推荐使用Hooks或其他组合模式来复用逻辑,同时减少嵌套。过多的HOC分层会导致“包装地狱”,组件包装层级过深,使调试变得困难,并使React DevTools变得混乱。像Recompose这样的库已不再维护,因为Hooks可以在很大程度上替代它们的功能。例如,你可以创建一个钩子来返回元素是否被悬停以及一个要绑定的ref,而不是使用useHoverHOC。这避免了额外的组件层,并且与React即将推出的功能更兼容——当逻辑不隐藏在HOC中时,React编译器可以更好地分析组件代码。withHover
graphql()graphql()graphql()graphql()graphql()useMutationgraphql()graphql()graphql()graphql()graphql()useMutationfunction withStyles(Component) {
return props => {
const style = { padding: '0.2rem', margin: '1rem' }
return <Component style={style} {...props} />
}
}
const Button = () = <button style={{ color: 'red' }}>Click me!</button>
const StyledButton = withStyles(Button)withStylesstyleButtonstylefunction withStyles(Component) {
return props => {
const style = {
padding: '0.2rem',
margin: '1rem',
...props.style
}
return <Component style={style} {...props} />
}
}
const Button = () = <button style={{ color: 'red' }}>Click me!</button>
const StyledButton = withStyles(Button)function withStyles(Component) {
return props => {
const style = { padding: '0.2rem', margin: '1rem' }
return <Component style={style} {...props} />
}
}
const Button = () = <button style={{ color: 'red' }}>Click me!</button>
const StyledButton = withStyles(Button)withStylesstyleButtonstylefunction withStyles(Component) {
return props => {
const style = {
padding: '0.2rem',
margin: '1rem',
...props.style
}
return <Component style={style} {...props} />
}
}
const Button = () = <button style={{ color: 'red' }}>Click me!</button>
const StyledButton = withStyles(Button)