简体   繁体   English

如何在 react-native 的类组件中使用 useTheme()?

[英]How Can I use useTheme() in class components in react-native?

Currently I want to use useTheme() but as its hook that I can not use it in class component.目前我想使用 useTheme() 但作为它的钩子,我不能在类组件中使用它。 But its my compulsion to use it in class components can any one tell me how can I use?但是我强制在类组件中使用它,谁能告诉我我该如何使用?

Without knowing what library you are using ( useTheme() is not part of React or React Native) it is hard to answer specific to your use case.如果不知道您使用的是什么库( useTheme()不是 React 或 React Native 的一部分),则很难针对您的用例做出具体回答。 Because hooks can only be used in Functional components, you can wrap your class component in one.因为钩子只能在功能组件中使用,所以你可以将你的类组件包装成一个。 For example:例如:

class MyComponent extends React.Component {
    render() {
        return <p>Hello!</p>
    }
}

export default function(props) {
    const theme = useTheme()

    return <MyComponent {...props} theme={theme}/>
}

React Navigation 没有说明替代方案,但您可以将类组件包装在函数组件中并将其作为道具传递

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM