// 高阶组件中使用Context
import React from 'react'
const ThemeContext =React.createContext('dark')
export function WithTheme(Component){
return function ThemedComponent(props){
return (
<ThemeContext.Consumer>
{theme=>
<Component {...props} theme={theme}></Component>
}
</ThemeContext.Consumer>
)
}
}
import {WithTheme} from "./contextHight"
import React from 'react'
function Button({theme, ...rest}) {
return <button className={theme} {...rest} />;
}
const ThemedButton = WithTheme(Button);
// 对这个组件进行高级包装
export default ThemedButton
网友评论