ReactJS: How can I have a more modular use of prop types & shapes for objects?

隐身守侯 提交于 2019-11-29 12:32:23

问题


I like to explicitly specify all my prop types for every class.

React.createClass({
  propTypes: {
    optionalArray: React.PropTypes.array,
    optionalBool: React.PropTypes.bool,
...

This is from reading reusable components: https://facebook.github.io/react/docs/reusable-components.html

However, what if I have a very common object that I use in many classes? For example:

var MemoryForm = React.createClass({
  propTypes: {
    memory: React.PropTypes.shape({
      memoryID: React.PropTypes.number,
      content: React.PropTypes.string,
      date: React.PropTypes.object,
      dateStr: React.PropTypes.string,
      note: React.PropTypes.string
    }).isRequired,
    ...

var MemoriesArea = React.createClass({
  propTypes: {
    // The initial memory to fill the memory form with.
    formMemory: React.PropTypes.shape({ // <== shape used again
      memoryID: React.PropTypes.number,
      content: React.PropTypes.string,
      date: React.PropTypes.object,
      dateStr: React.PropTypes.string,
      note: React.PropTypes.string
    }).isRequired,
    // ...



var Playground = React.createClass({
  getInitialState: function() {
    var initVars = {
      // The initial memory to fill the memory form with.
      formMemory: {  // <== shape is used again.
        memoryID: 0,
        content: "",
        date: null,
        dateStr: "",
        note: ""
      }
    };
    return initVars;
  }
  //...

Here, I use the 'memory' shape very frequently in the prop types for various classes, as well as in some initializations. How can make this more DRY - i.e., less code duplication, so a change to this object shape will be more maintainable in the future?


回答1:


I had the same problem and just moved the values to a separate ES6 module. In your example:

// File lib/PropTypeValues.js
import { PropTypes } from 'react';

export let MemoryPropTypes = PropTypes.shape({
  memoryID: PropTypes.number,
  content: PropTypes.string,
  date: PropTypes.object,
  dateStr: PropTypes.string,
  note: PropTypes.string
}).isRequired

Then in your client code:

// MemoryForm.jsx
import { MemoryPropTypes } from './lib/PropTypeValues'
import React from 'react';

class MemoryForm extends React.Component {
  static propTypes: {
    memory: MemoryPropTypes,
    // ...
  };
}

Hope this helps.




回答2:


I would make a small module exposing that functionality. It would look something like this in a CommonJS world:

let React = require('react');

module.exports = {
  propTypes() {
    return React.PropTypes.shape({
      memoryID: React.PropTypes.number,
      content: React.PropTypes.string,
      date: React.PropTypes.object,
      dateStr: React.PropTypes.string,
      note: React.PropTypes.string
    }).isRequired;
  },
  initialValues() {
    return {
      memoryID: 0,
      content: "",
      date: null,
      dateStr: "",
      note: ""
    };
  }
}

Then you'd use that in components like this:

let memoryUtils = require('./memory-utils');

let MyComponent = React.createClass({
  propTypes: memoryUtils.propTypes(),
  render() {
    ...
  }
});

And:

let memoryUtils = require('./memory-utils');

let MyComponent = React.createClass({
  getInitialState() {
    return memoryUtils.initialValues();
  },
  render() {
    ...
  }
});


来源:https://stackoverflow.com/questions/29912334/reactjs-how-can-i-have-a-more-modular-use-of-prop-types-shapes-for-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!