question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PropTypes outside of React. Independent type checks module

See original GitHub issue

using react PropTypes is awesome, and can help catching errors at the early stage, but sometimes, when the application is pretty large, there is always a flow of data structures of a big size. Like in the web email client application, there can be a Message type that is passed all over the place, and multiple components can accept it in props.

so the solution to that is usually creating a type file. e.g.

// types/message.js

import React from 'react';

export default React.PropTypes.shape({
    id: React.PropTypes.string.isRequired,
    subject: React.PropTypes.string
}).isRequired;

and then reusing this type in components that get messages in props


import messageType from './types/message';

MessagePreview.PropTypes = {
  message: messageType
}

but sometimes this data flows in some other than components elements. for example flux stores, or action creators. And that usually requires duplication of types (using immutable.js records or similar)

That would we really nice if we could use this type checks in other parts of the application.

for example

// stores/message.js

import messageType from './types/message';
import checkTypes from 'react/check-types';

/**
 * @param {Array<Object>} payload.messages the array of message objects received from the API
 */
onDataReceived = (payload) => {
  payload.messages.forEach((message) => {
    checkTypes(message, messageType);
    addToStore(message);
  });
}

will this be any good? I guess architectually it means making propTypes.js more independent, and creating an adapter for prop/context validating

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
ArmorDarkscommented, Feb 18, 2017

I know this is old issue, but it’s sad to see it closed. I think I’m not the only one who’d like to use PropTypes in non-React projects too.

Did anything change during last year?

2reactions
sophiebitscommented, Sep 23, 2015

FWIW we use https://github.com/reactjs/react-docgen at Facebook.

We’re not going to build this as part of React but perhaps something like Flow will have optional runtime typechecks eventually which could incorporate some or all of this functionality.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typechecking With PropTypes - React
To run typechecking on the props for a component, you can assign the special propTypes property: import PropTypes from 'prop-types'; class Greeting extends ......
Read more >
Using PropTypes Outside of React in Template Literal ...
I recently moved away from using JSX as my templating system and switched to using “components” of template literals in JavaScript.
Read more >
React eslint error missing in props validation - Stack Overflow
I have the next code, eslint throw: react/prop-types onClickOut; is missing in props ...
Read more >
How To Customize React Components with Props
PropTypes are a simple type system to check that data matches the expected types during runtime. They serve as both documentation and an ......
Read more >
How to validate React props using PropTypes - LogRocket Blog
Since JavaScript doesn't have a built-in type checking solution, ... recommend checking out the following video tutorial on React PropTypes:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found