-
Notifications
You must be signed in to change notification settings - Fork 49.5k
Description
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