1

Trying to extend react-native View component. However I'm having trouble extending props of the View component.

Here is how I extend the Props

import { StyleSheet, View, ViewStyle, ViewProps } from "react-native";
interface Props extends ViewProps {
  pb?: keyof Size | number;
  pt?: keyof Size | number;
  pv?: keyof Size | number;
  ph?: keyof Size | number;
}

Here is my custom View component

class WrappedView extends Component<Props> {
  render() {
    const methods = {};
    let { style: attrStyles, children, pb, pt, pv, ph, ...rest } = this.props;
    let style = [attrStyles];
    // Shorthand prop styles
    let propStyles: ViewStyle = {};
    propStyles.paddingBottom = pb;
    propStyles.paddingTop = pt;
    propStyles.paddingVertical = pv;
    propStyles.paddingHorizontal = ph;
    style.push(propStyles);
    return (
      <View style={style} {...rest} {...this.state} {...methods}>
        {children}
      </View>
    );
  }
}

When I try a test like so

const test = () => {
  return <WrappedView pb={"large"} width={"100%"} />;
};

I get the following issue

Type '{ pb: "large"; width: string; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<WrappedView> & Readonly<Props> & Readonly<{ children?: ReactNode; }>'.
  Property 'width' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<WrappedView> & Readonly<Props> & Readonly<{ children?: ReactNode; }>'.
2
  • width is not a prop from ViewProps. Check it here Commented May 8, 2019 at 10:55
  • 1
    Argh this is pretty embarrassing thanks Commented May 8, 2019 at 10:58

1 Answer 1

2

The code in the question does indeed extend the props correctly instead it was a client mistake. The client should read:

const test = () => {
  return <WrappedView pb={"large"} style={{width:"100%"}} />;
};
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly. Note that you don't need to redefine style prop in your interface Props extends ViewProps as you did in the question, 'cause style is already a ViewProp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.