react-proptypes

How to prevent building app if component prop types are invalid?

扶醉桌前 提交于 2021-02-07 05:09:58
问题 Here is a example for PropTypes: import PropTypes from 'prop-types'; class Greeting extends React.Component { render() { return ( <h1>Hello, {this.props.name}</h1> ); } } Greeting.propTypes = { name: PropTypes.string }; Now I use Greeting component as: <Greetings otherProp="this is a invalid prop" /> In this case, when I build app for deployment, no error is thrown & app is successfully built. But it give error in runtime & breaks down. How can I add check to remove this problem & ensure no

How to prevent building app if component prop types are invalid?

守給你的承諾、 提交于 2021-02-07 05:09:03
问题 Here is a example for PropTypes: import PropTypes from 'prop-types'; class Greeting extends React.Component { render() { return ( <h1>Hello, {this.props.name}</h1> ); } } Greeting.propTypes = { name: PropTypes.string }; Now I use Greeting component as: <Greetings otherProp="this is a invalid prop" /> In this case, when I build app for deployment, no error is thrown & app is successfully built. But it give error in runtime & breaks down. How can I add check to remove this problem & ensure no

How to prevent building app if component prop types are invalid?

夙愿已清 提交于 2021-02-07 05:04:54
问题 Here is a example for PropTypes: import PropTypes from 'prop-types'; class Greeting extends React.Component { render() { return ( <h1>Hello, {this.props.name}</h1> ); } } Greeting.propTypes = { name: PropTypes.string }; Now I use Greeting component as: <Greetings otherProp="this is a invalid prop" /> In this case, when I build app for deployment, no error is thrown & app is successfully built. But it give error in runtime & breaks down. How can I add check to remove this problem & ensure no

PropTypes doesn't work in React

笑着哭i 提交于 2021-01-27 07:39:44
问题 I am running React 16.2.0 and I am using PropTypes 15.6.1. I am using ES6 syntax and Webpack. I am trying to make PropTypes throw a warning when I pass invalid props, but it doesn't work. This is the code: SimpleMessage.js import React from "react" import PropTypes from "prop-types" class SimpleMessage extends React.Component { render() { return( <p>{this.props.message}</p> ) } } SimpleMessage.propTypes = { message: PropTypes.func } export default SimpleMessage index.js import React from

react-native prop type for text style

╄→尐↘猪︶ㄣ 提交于 2020-01-31 03:42:07
问题 i have component with a simple structure and a <Text> somewhere inside the tree for which i want to pass in a style. Which works perfectly but for the proptypes validation. The basic setup is not much more than that export default class Component extends PureComponent { render() { return (<View><Text style={this.props.style}>Some text</Text></view>); } } Component.defaultProps = { style: null, }; Component.propTypes = { style: ViewPropTypes.style, }; The problem is that the ViewPropTypes

Is it possible to use PropTypes to validate Dictionary-like objects?

邮差的信 提交于 2019-12-23 09:18:10
问题 I need to validate dictionary-like objects in my reducers, but since I'm already using Babel I don't want to resort to tools like Typescript. Take this object as an example: posts : { byId : { "post1" : { id : "post1", author : "user1", body : "......", comments : ["comment1", "comment2"] }, "post2" : { id : "post2", author : "user2", body : "......", comments : ["comment3", "comment4", "comment5"] } } allIds : ["post1", "post2"] } How could I express my expectations for the byId object using

Eslint/PropType Issues with Composed Render Props

喜你入骨 提交于 2019-12-23 05:34:11
问题 Using the library pedronauck/react-adopt, I'm composing a couple render prop functions related to Apollo & Formik. My eslint rules are complaining that I'm missing display name and render is missing prop validation . Anyone know how to resolve these issues? 来源: https://stackoverflow.com/questions/54663177/eslint-proptype-issues-with-composed-render-props

PropTypes in a TypeScript React Application

邮差的信 提交于 2019-12-17 15:24:24
问题 Does using React.PropTypes make sense in a TypeScript React Application or is this just a case of "belt and suspenders"? Since the component class is declared with a Props type parameter: interface Props { // ... } export class MyComponent extends React.Component<Props, any> { ... } is there any real benefit to adding static propTypes { myProp: React.PropTypes.string } to the class definition? 回答1: There's usually not much value to maintaining both your component props as TypeScript types and

How to test a React component propTypes validation

时光总嘲笑我的痴心妄想 提交于 2019-12-13 03:36:09
问题 How can I unit test the propTypes validation of a React component for PropTypes.shape ? My component propTypes validation: MyComponent.propTypes = { propA: PropTypes.string.isRequired, propB: PropTypes.shape({ inner1: PropTypes.bool.isRequired, inner2: PropTypes.func, }).isRequired, } My unit test: describe('MyComponent propTypes validation', () => { it('validates propA', () => { expect(MyComponent.propTypes.propA).toBe(PropTypes.string.isRequired); }) // THIS FAILS. Expected: [Function bound

React composition component proptypes

依然范特西╮ 提交于 2019-12-12 13:55:06
问题 I'm using the composition pattern for react. So, for example, I have this simple component: class Simple extends React.Component { render() { return <div>{this.props.text}</div>; } } Simple.propTypes = { text: React.PropTypes.string.isRequired }; Now, I have this enhanced component: class Enhanced extends React.Component { render() { return ( <div> <div>Hi, I'm the enhanced version</div> <Simple {...this.props} /> </div> ); } } Now, how can I specify the proptypes for the enhanced component?