简体   繁体   English

使用redux,反模式时,在react叶子组件中设置this.state? 优点/缺点

[英]setting this.state in react leaf component when using redux, anti-pattern? benefits / disadvantages

Is setting and keeping the state in a Leaf component that doesn't ever touch Redux an antipattern? 在从未接触过Redux的Leaf组件中设置和保持状态是否是反模式?

For example, suppose a project has a leaf node with some states like modalIsOpen and buttonIsDisabled and countOfObjects . 例如,假设一个项目的叶子节点具有一些状态,如modalIsOpenbuttonIsDisabled以及countOfObjects

This leaf node has functions that keep track of these states and modifies them as necessary. 该叶节点具有跟踪这些状态并根据需要对其进行修改的功能。 In this way, it's a simple react component before Redux came along. 这样,它是Redux出现之前的简单反应组件。 It's completely modular and reusable since you don't have to reimplement any Redux Actions such as a toggleModal function that you would need to connect to your component if you were to throw this component in another project. 它是完全模块化的和可重用的,因为您无需重新实现任何Redux操作(例如toggleModal函数),如果要将这个组件扔到另一个项目中,则需要connect到该组件。 (Let's assume no other components need these simple states) (假设没有其他组件需要这些简单状态)

So given this example, is this an antipattern? 所以给定这个例子,这是反模式吗? What are the benefits and disadvantages of doing it this way? 这样做有什么好处和坏处? I want to assess any benefits + disadvantages before I decide if I want to make my leaf nodes like this. 在决定是否要像这样创建叶节点之前,我想先评估任何利弊。

It's not an antipattern. 这不是反模式。 You can use state in Leaf components if it's affects only on visual template of component and not related to some shared data. 如果状态仅影响组件的可视模板并且与某些共享数据无关,则可以在Leaf组件中使用状态。 So, do not be very fanatic about Redux patterns and React-ways. 因此,不要对Redux模式和React-way非常狂热。 Write elegant and readable code, which can be supported by you and your teammates after the time, and make a product)) 编写精美且易读的代码,此后您和您的团队成员将为您提供支持,并制作出产品))

Absolutely not! 绝对不! Redux is great for managing application data, but when it comes to managing UI related state such as "active" class, it's advised that you use React's native state. Redux非常适合管理应用程序数据,但是在管理与UI相关的状态(例如“活动”类)时,建议您使用React的本机状态。

For example- If you want to store state of a button, if it is enabled or disabled, you won't necessarily need Redux for that! 例如-如果要存储按钮的状态,启用还是禁用,则不一定需要Redux! You can still use React's native state. 您仍然可以使用React的本机状态。 Like this- 像这样-

class Button extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      active: true
    };
    this.toggle = this.toggle.bind(this);
  }

  toggle() {
    this.setState({
      active: !this.state.active
    });
  }

  render() {
    return (
      <div>
        <button disabled={this.state.active} onClick={this.toggle}>
          {this.state.active ? 'Click me!' : 'You cannot click me'}
        </button>
      <div>
    );
  }
}

See? 看到? You don't need Redux here at all! 您根本不需要Redux! Good luck! 祝好运!

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

相关问题 在componentWillReceiveProps中反应设置状态-对于此组件和父组件-反模式? - React setting state in componentWillReceiveProps - for this component and parent component - anti-pattern? 想知道使用状态(this.state)的反应组件(而不是redux)的缺点 - want to know the disadvantages of using state (this.state) react components (not redux) 以Redux状态存储React元素:反模式? - Storing a React element in Redux state: anti-pattern? 在React组件中设置state和props之外的值是否是反模式? - Is setting a value outside of both state and props in a React component an anti-pattern? 使用Redux时,将事件从表示形式传递到容器组件是否是反模式? - Is passing an event from a presentational to container component an anti-pattern when using Redux? 当一个 state 可以完全确定另一个 state 时,在 React 中使用两个状态是否是一种反模式? - Is using two states in React an anti-pattern when one state can entirely determine the other state? 使用Redux时,在React视图中保留一些条件代码是否是反模式? - Is it an anti-pattern to keep some conditional code in React views when using Redux? 反应组件加载模式(或反模式?) - React component loading pattern (or anti-pattern?) React:正在访问儿童的反模式状态? - React: is accessing the state of children an anti-pattern? 这是一个 React 反模式吗? - Is this a React anti-pattern?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM