Comments (9)
You can certainly do propType validation on pure function components:
import React, { PropTypes } from 'react'
Foo.propTypes = {
bar: PropTypes.string.isRequired
}
export default function Foo ({ bar }) {
return (
<div className='Foo'>
Foo is {bar}
</div>
)
}
I find it very useful. It forces you to declare your component's API up-front.
from eslint-config-standard-react.
While I would put Foo.propTypes
after the export
, @cesarandreu is 100% correct in that this is a solved problem.
from eslint-config-standard-react.
@cesarandreu thank you, I wasn't aware of that!
from eslint-config-standard-react.
This won't allow "props" that we wouldn't normally declare such as "key" to be passed in will it?
from eslint-config-standard-react.
@hxpaul what do you mean?
This behaves as any other definition of propTypes
would.
from eslint-config-standard-react.
Think I might be answering my own question as I try and explain the problem. Here's a quick (fake) example, so hope I've not left a synax error in here
In @cesarandreu's example above this wouldn't work would it?
['a', 'b', 'c'].map((bar, index) => {
return <Foo bar={bar} key={index} />;
})
If I do this, it doesn't lint, but if I were to pass in key
(say I'm calling this in a loop) it'll be accepted and used with no warnings. This works but doesn't lint:
const Anchor = (props) => {
props.className = 'foo';
return (
<a {...props}>
{children}
</a>
);
};
Anchor.propTypes = {
children: React.PropTypes.any.isRequired
};
// <Anchor>hey</Anchor> renders <a class="foo" data-reactid=".0">hey</a>
// <Anchor key="bar">hey</Anchor> renders <a class="foo" data-reactid=".0.bar">hey</a> or similar
if I desctructure my props I have to explicitly name key
now. Not a problem now I say it out loud, but I didn't have to think about key
before, someone could just use it.
const Anchor = ({ children, href, key, onClick}) => {
return (
<a className="foo" href={href} key={key} onClick={onClick}>
{children}
</a>
);
};
I've maybe stumbled into the wrong repo with my comments here, if so apologies, was googling for "Property Type Validation in React.js Stateless Functions"
from eslint-config-standard-react.
key
is explicitly implicitly handled by react
? Are you saying react
throws a warning right now if you don't define it?
Perhaps it has an implicit propType
?
from eslint-config-standard-react.
maybe I'm wrong, I couldn't see how key
could work with destructured props as in cesarandreu's example, say
['a', 'b', 'c'].map((bar, index) => {
return <Foo bar={bar} key={index} />;
})
from eslint-config-standard-react.
@hxpaul I don't think you'll even receive key
in props unless you define it in the propTypes
. YMMV, been a while since I've tried that.
from eslint-config-standard-react.
Related Issues (20)
- Add rule react/no-unused-prop-types HOT 1
- modify rule jsx-no-bind HOT 5
- Unexpected error comes out when everything is ok HOT 1
- Fails to do stuff HOT 1
- Using stateless function through function invoking way hints me missing props validation. HOT 1
- Unmet peer dependency when installing a fresh create-react-app application HOT 6
- 16.2.0 new Feature
- error using Class Properties with React HOT 1
- DeprecationWarning: The 'parserOptions.ecmaFeatures.experimentalObjectRestSpread' option is deprecated. Use 'parserOptions.ecmaVersion' instead. HOT 4
- How to use? HOT 1
- no-did-update-set-state should not throw an error since React 16.4 HOT 2
- Shouldn't this lib follow the React rules in Standard? HOT 1
- Make work with install-peerdeps
- Include React Hooks rules (eslint-plugin-react-hooks) HOT 13
- An in-range update of eslint-plugin-react is breaking the build ๐จ HOT 2
- Error - .eslintrc.js ยป eslint-config-standard-react HOT 3
- Adding React Hooks rules HOT 2
- Support ESLint 8
- Incompatibility with latest eslint HOT 1
- Release the ESLint 8 version HOT 16
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eslint-config-standard-react.