Coder Social home page Coder Social logo

Comments (9)

cesarandreu avatar cesarandreu commented on May 18, 2024 60

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.

dcousens avatar dcousens commented on May 18, 2024 4

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.

jprichardson avatar jprichardson commented on May 18, 2024

@cesarandreu thank you, I wasn't aware of that!

from eslint-config-standard-react.

hxpaul avatar hxpaul commented on May 18, 2024

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.

dcousens avatar dcousens commented on May 18, 2024

@hxpaul what do you mean?
This behaves as any other definition of propTypes would.

from eslint-config-standard-react.

hxpaul avatar hxpaul commented on May 18, 2024

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.

dcousens avatar dcousens commented on May 18, 2024

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.

hxpaul avatar hxpaul commented on May 18, 2024

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.

dcousens avatar dcousens commented on May 18, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.