73

With the forbid-prop-types rule enabled, eslint forbids me from using style: React.PropTypes.object, and using shape is suggested.

But is it really necessary to define all the available properties there for this purpose like this?

DEMO.propTypes = {
    style: React.PropTypes.shape({
        color: React.PropTypes.string,
        fontSize: React.PropTypes.number,
        ...
    })
};

Too much definition code!

NiFi
  • 2,398
  • 1
  • 17
  • 26
Howard
  • 4,474
  • 6
  • 29
  • 42
  • Future readers, let me save you some time, see the answer by [@David Weldon](https://stackoverflow.com/a/39627920/812919) – olfek Sep 25 '18 at 11:00

10 Answers10

76

React Native now contains ViewPropTypes, which will replace View.propTypes.style. Example of usage:

import { ViewPropTypes } from 'react-native';

MyComponent.propTypes = {
  style: ViewPropTypes.style
};
NiFi
  • 2,398
  • 1
  • 17
  • 26
  • 8
    Have you seen an equivalent for Text? TextPropTypes doesn't seem to exist in the React Native codebase View.propTypes no longer exists at run-time in release mode, am not sure yet whether Text.propTypes does hthough. – Ben Clayton Dec 05 '17 at 13:34
  • @BenClayton I have not. The RN documentation for ViewPropTypes isn’t available atm either. – NiFi Dec 05 '17 at 19:28
  • 2
    Its deprecated for react-native-web https://github.com/necolas/react-native-web/releases/tag/0.12.0 – masoud soroush Jun 20 '20 at 13:44
38
View.propTypes.style

or

Text.propTypes.style

It would look like this:

DEMO.propTypes = {
    style: Text.propTypes.style,
    ...
};

https://facebook.github.io/react-native/releases/0.21/docs/style.html#pass-styles-around

Uilque Messias
  • 281
  • 2
  • 17
tiagob
  • 1,088
  • 1
  • 11
  • 14
21

One possibility is to use the react-style-proptype package like so:

import stylePropType from 'react-style-proptype';

MyComponent.propTypes = {
  style: stylePropType,
};
David Weldon
  • 63,632
  • 11
  • 148
  • 146
16

Use the simple code as below.

PropTypes.oneOfType([PropTypes.object, PropTypes.array])

So, you can apply to this your code.

DEMO.propTypes = {
    style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
};

Tested in RN > 0.57.

Jeff Gu Kang
  • 4,749
  • 2
  • 36
  • 44
4

Use ViewPropTypes.style. View.propTypes has been deprecated

Matt Stow
  • 6,053
  • 6
  • 24
  • 26
4

Quick and dirty / Hack-ish

Extending @jalal246 answer:

PropTypes.objectOf(PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.number,
]))

This can be used for web styles (refer other answers for React-native) without having to use other package for it.

Vivek
  • 4,786
  • 2
  • 18
  • 27
3

As some of the former answers are depricated, so I will share my solution here.

In fact, you could simply import the original types, such as ViewStyle, TextStyle, ImageStyle, FlexStyle & ImageStyle from react-native.

For example:

import React from 'react';
import { TouchableOpacity, ViewStyle } from 'react-native';

interface IProps {
    style?: ViewStyle
}

<TouchableOpacity style={[props.style]}>
    ...
</TouchableOpacity>

Remarks: If you are using 3-rd party libraries that supports TypeScript, there should be relevant style property as well, just like in the example above.

louielyl
  • 745
  • 7
  • 9
0

I usually use PropTypes.objectOf(PropTypes.string) no need to define all css style.

Jalal
  • 3,308
  • 4
  • 35
  • 43
0

There are two possibilities. The first one, which I prefer, is to use ViewPropTypes from react. The other one, using PropTypes, is:

Component.propTypes = {
    textStyles: PropTypes.oneOfType([
        PropTypes.object,
        PropTypes.number
    ])
}

You may ask "why PropTypes.number if a style is just an object?" the answer is that the react native team made a little optimization which caches the stylesheet and simply sends the cached ID.

0

There's an easy way with JSDoc.

/**
   * @typedef Props
   * @prop {React.CSSProperties} style
   */

/**
 * some component
 * @augments {Component<Props, State>}
 */

 export default class SomeRandomClass extends React.Component { }

Adding above JSDoc to your react component will enable VS Code to suggest all possible CSS properties.

djsdev
  • 76
  • 2
  • 4