Skip to content

fixed unnecessary ThemProps in withTheme hoc - #2147

Merged
iRoachie merged 1 commit into
react-native-elements:patchfrom
roadmanfong:next
Oct 29, 2019
Merged

fixed unnecessary ThemProps in withTheme hoc#2147
iRoachie merged 1 commit into
react-native-elements:patchfrom
roadmanfong:next

Conversation

@roadmanfong

Copy link
Copy Markdown
Contributor

when I use withTheme

import React from 'react'
import {ThemeProps, withTheme } from 'react-native-elements'

interface Props extends ThemeProps<any>{
   onPress: () => void
}

class Button extends React.Component {
   // ... rest of all
}

const ThemeButton = withTheme(Button)
<ThemeButton 
  onPress={this.onPress}
/>

keep showing error about not providing required props theme updateTheme replaceTheme

@iRoachie

Copy link
Copy Markdown
Collaborator

What are you trying to do? I'm not understanding what bug you're having

@roadmanfong

Copy link
Copy Markdown
Contributor Author

I think withTheme is an injector according this article https://medium.com/@jrwebdev/react-higher-order-component-patterns-in-typescript-42278f7590fb

const ThemedComponent = withTheme(Component)

theme updateTheme replaceTheme are already injected by withTheme should not be a required props of ThemedComponent, but currently the definition treat these as required props

@iRoachie

Copy link
Copy Markdown
Collaborator

That's not true. withTheme works as expected.

interface MyTextProps {
  name: string;
}

const MyText = withTheme<MyTextProps>(({ theme, name }) => (
  <Text style={{ color: theme.colors.primary }}>{name}</Text>
));

const Main = () => {
  return (
    <View style={styles.container}>
      <MyText name="Mary" />
    </View>
  );
};

Example without component props:

const MyText = withTheme(({ theme }) => (
  <Text style={{ color: theme.colors.primary }}>Mary</Text>
));

const Main = () => {
  return (
    <View style={styles.container}>
      <MyText />
    </View>
  );
};

@roadmanfong

Copy link
Copy Markdown
Contributor Author

Yes it works well in function component, but class component shows error

interface Props extends ThemeProps<any> {
  name: string
}

class MyText2 extends Component<Props> {
  public render() {
    const {
      name
      theme,
    } = this.props
    return (
      <Text style={{ color: theme.colors.primary }}>{name}</Text>
    )
  }
}

const ThemedMyText2 = withTheme(MyText2)

const Main2 = () => {
  return (
    <View style={styles.container}>
      <ThemedMyText2 name="Mary" />
    </View>
  );
}

@iRoachie

iRoachie commented Oct 29, 2019

Copy link
Copy Markdown
Collaborator

Ahh okay, it'll fail because you didn't use the generic on withTheme. But I can still accept this 👍

@iRoachie
iRoachie changed the base branch from next to patch October 29, 2019 08:01
@react-native-elements react-native-elements deleted a comment from codecov Bot Oct 29, 2019
@iRoachie
iRoachie merged commit c77f12c into react-native-elements:patch Oct 29, 2019
@iRoachie

Copy link
Copy Markdown
Collaborator

Thanks again 💯

@iRoachie

Copy link
Copy Markdown
Collaborator

Live in 1.2.7

@roadmanfong

Copy link
Copy Markdown
Contributor Author

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants