How to declare style in propTypes?

React NativeEslint

React Native Problem Overview


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!

React Native Solutions


Solution 1 - React Native

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

import { ViewPropTypes } from 'react-native';

MyComponent.propTypes = {
  style: ViewPropTypes.style
};

Solution 2 - React Native

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

Solution 3 - React Native

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

import stylePropType from 'react-style-proptype';

MyComponent.propTypes = {
  style: stylePropType,
};

Solution 4 - React Native

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.

Solution 5 - React Native

Use ViewPropTypes.style. View.propTypes has been deprecated

Solution 6 - React Native

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.

Solution 7 - React Native

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.

Solution 8 - React Native

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

Solution 9 - React Native

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.

Solution 10 - React Native

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

You could simply apply the type ViewStyle, TextStyle, ImageStyle, FlexStyle & ImageStyle to the style interface.

For example:

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

interface IProps {
    style?: ViewStyle
}

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

Remarks: Some components has its required type for the style property, just like in the exmaple above, the type of props.style cannot be TextStyle or others but ViewStyle, hope that helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHowardView Question on Stackoverflow
Solution 1 - React NativeNiFiView Answer on Stackoverflow
Solution 2 - React NativetiagobView Answer on Stackoverflow
Solution 3 - React NativeDavid WeldonView Answer on Stackoverflow
Solution 4 - React NativeJeff Gu KangView Answer on Stackoverflow
Solution 5 - React NativeMatt StowView Answer on Stackoverflow
Solution 6 - React NativeVivekView Answer on Stackoverflow
Solution 7 - React NativeVictorView Answer on Stackoverflow
Solution 8 - React NativeJalalView Answer on Stackoverflow
Solution 9 - React NativedjsdevView Answer on Stackoverflow
Solution 10 - React NativelouielylView Answer on Stackoverflow