简体   繁体   English

关于PropTypes位置的Redux最佳做法

[英]Redux best practices for PropTypes location

was wondering if it's better to put propType definitions in the containers with all the prop logic or the presenter where the props will actually be used. 我想知道是否最好将propType定义放到具有所有prop逻辑的容器中或将实际使用prop的presenter中。 I can see arguments for both. 我可以看到两个参数。

In the containers allows you to keep track of all the prop logic all in one place while in the presenters confirms they will be used properly. 在容器中,您可以将所有道具逻辑都集中在一处,而在演示者中,确认它们将被正确使用。

Thanks for the input. 感谢您的输入。

Update for clarity 为清楚起见进行更新

example-container.js example-container.js

import PropTypes from 'prop-types';
import { connect } from 'react-redux';

import ButtonPresenter from './some/file';

const mapStateToProps = (store, props) => {
  return ({
    example: Number(5),
  });
};

const mapDispatchToProps = (dispatch, props) => ({
  onClick: id => { console.log(id) },
});

ButtonPresenter.propTypes = {
  onClick: PropTypes.func.isRequired,
  example: PropTypes.number.isRequired,
}

const ButtonContainer = connect(mapStateToProps, mapDispatchToProps)(BoxPresenter);

export default ButtonContainer;

pros 优点

all logic is in 1 location 所有逻辑都在1个位置

2 different containers could work with a presenter 主持人可以使用2个不同的容器

cons 缺点

presenter may need a type not known by just the container array for map 演示者可能需要一种仅由容器数组未知的类型

example-presenter.js example-presenter.js

import React from 'react';
import PropTypes from 'prop-types';

const ButtonPresenter = ({example, onClick}) => {
  return (
    <button onClick={onClick}>{example}</button>
  );
};

ButtonPresenter.propTypes = {
  onClick: PropTypes.func.isRequired,
  example: PropTypes.number.isRequired,
}

export default ButtonPresenter;

pros 优点

propTypes written in 1 place for everything using the presenter 使用主持人在所有地方在1个地方编写的propTypes

cons 缺点

not as flexable, propTypes could be seen as logic then logic is in both container and presenters 不那么灵活,propTypes可以看作是逻辑,然后逻辑既在容器中又在演示者中

If it's a Class Component , I like to leave the propTypes and defaultProps at the very top after the class creation: 如果它是一个Class Component ,我想在创建类之后将propTypes和defaultProps放在最顶部:

import React from 'react';
import PropTypes from 'prop-types';

class MyComponent extends React.Component {
    static propTypes = {
        prop1: PropTypes.string.isRequired,
        prop2: PropTypes.bool,
        prop3: PropTypes.func
    };

    defaultProps = {
        prop2: false,
        prop3: () => {}
    };

    constructor() {}
    render() {}
}

export default MyComponent;

Or for a Functional Component , I like to create the variables and leave them after the imports: 或对于功能组件 ,我喜欢创建变量并将其保留在导入之后:

import React from 'react';
import PropTypes from 'prop-types';

const propTypes = {
    prop1: PropTypes.string.isRequired,
    prop2: PropTypes.bool,
    prop3: PropTypes.func
};

const defaultProps = {
    prop2: false,
    prop3: () => {}
};

const MyComponent = props => {

}

MyComponent.propTypes = propTypes;
MyComponent.defaultProps = defaultProps;

export default MyComponent;

So it makes very clear what you should pass to the component via props. 这样就很清楚您应该通过prop传递给组件什么。 It's kind of a Summary for your component. 这是您组件的摘要。

I don't see the relation between Redux and PropTypes on your question. 在您的问题上,我看不到Redux和PropTypes之间的关系。

Anyway I would specify PropTypes on any React component that would be likely to be reused/consumed. 无论如何,我会在可能被重用/消耗的任何React组件上指定PropTypes It doesn't make sense to me to do it on the highest components that are bound to the store 对我而言,对商店绑定的最高组件执行此操作没有任何意义

This could be opinion based though 虽然这可能是基于意见的

[edited:] [编辑:]
I might be missing something but afaik only the root component (higher on the hierarchy) should be bound to the store. 我可能会丢失一些东西,但是afaik只能将根组件(层次结构中较高的组件)绑定到商店。 Therefore I don't see how you are going to consume it anywhere else and thus pass properties to it. 因此,我看不到您将如何在其他任何地方使用它并将属性传递给它。 Summarising, only the second snippet makes sense to me, the API of the component is self contained within the file, making the contract clear. 总而言之,只有第二个片段对我有意义,该组件的API自包含在文件中,使合同清晰明了。 eiffel.com/values/design-by-contract eiffel.com/values/design-by-contract

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM