简体   繁体   English

使用Redux时,在React视图中保留一些条件代码是否是反模式?

[英]Is it an anti-pattern to keep some conditional code in React views when using Redux?

I am fairly comfortable using React but I am confused about what is the right way to do this when using Redux. 我对使用React非常满意,但是对于使用Redux时执行此操作的正确方法感到困惑。 Everyone out there seems to be doing things their own way. 外面的每个人似乎都是以自己的方式做事。 On a button click I call a method in React view like: 在按钮上单击,我在React视图中调用一个方法,例如:

handleClick(value) {
  this.props.action.handleValueChange(value);
}

Now I want to do a lot more things on this button click. 现在,我想在此按钮单击上做更多的事情。 So should I do those things in the triggered action or in the handleClick method itself? 那我应该在触发的动作中还是在handleClick方法本身中做那些事情?

handleClick(value) {
  this.props.action.handleValueChange(value);
  // is this fine?
  if(this.props.someValue === "somethign")
    this.props.action.fetchSomeData(value);
}

I can also use thunk and do this in the action creator itself, but what is the recommended way of doing such things? 我还可以在动作创建者本身中使用thunk并执行此操作,但是推荐的处理方式是什么?

Think of it in terms of single responsibility and pure functions. 从单一责任和纯功能的角度考虑它。 That method should handle the click and translate it into the next hoop, like passing a baton in a relay race. 该方法应处理咔嗒声并将其转换为下一个箍,就像在接力赛中传递接力棒一样。 The click can cause an action or signal a state change. 点击可能会导致动作或发出状态更改信号。 You can notice times when things are getting too logic heavy when you start using the word and (it's like your Batman signal to modularize): 当您开始使用单词and时,您会注意到当事情变得逻辑上过于繁琐的时候(就像您将Batman信号模块化):

  • Get user input 获取用户输入
  • and save it to component state 并保存到组件状态
  • and call action creator 并致电行动创建者
  • and get the response back 并得到回应
  • and save that into some new state 并将其保存到新状态
  • and re-render the view 并重新渲染视图

Consider making a "dumb" function for everything you can call for each thing you need done. 考虑为每项需要完成的事情调用一个“哑巴”功能。 Where they reside exactly is up to you. 它们的确切位置取决于您。 It's the same logic as when you remember learning about database calls and "dumb models" that don't care what you do with the data, they just get exactly what they were asked to. 这与您记住有关数据库调用和“哑模型”的知识一样,而它们并不关心您对数据做些什么,它们只是得到了要求的逻辑。 Give them input, like "get user id 1337" and they respond "here {}" or "no". 给他们输入内容,例如“获取用户ID 1337”,然后他们回答“这里{}”或“否”。 They have one responsibility, and if you refactor everything around them, they hold their ground until the day "get user by id" is no longer sufficient. 他们负有一项责任,如果您重构他们周围的所有事物,他们将坚守自己的立场,直到“按ID获取用户”这一天不再足够。

This will make your apps nicely modular if you aim for the pureness. 如果您追求纯净,这将使您的应用程序具有很好的模块化性。 It could show up as nicely segmented methods in your class, or you could import each function in. If you want to do further reading, you will get a lot out of high-level articles and videos about functional programming. 它可以在您的类中显示为细分的方法,也可以将每个函数导入其中。如果您想进一步阅读,您将获得许多有关函数式编程的高级文章和视频。 Listen to the things that are important to them. 听对他们来说重要的事情。 You will notice it allows for great modular architecture with lots of small pieces of code coming together that are reuseable. 您会注意到,它允许使用大量可重用的小段代码,实现出色的模块化体系结构。 It is functional composition but it is also object composition. 它是功能组成,但也是对象组成。 These things are never-more relevant in React also since you could create your whole app in one class if you wanted to :) 这些事情在React中也不再相关,因为如果您愿意的话,您可以在一个类中创建整个应用程序:)

Redux-thunk fits in because it's app-level state. 适合Redux的应用程序是因为它处于应用程序级状态。 There is component-level state as well, and not just container state. 也有组件级状态,而不仅仅是容器状态。 When you pass props into a dumb component, it works with these props as its "state", so its like child #2. 当您将道具传递到一个哑巴组件时,它将这些道具作为其“状态”使用,因此它就像孩子#2。 I trust you are thinking about hierarchies now. 我相信您现在正在考虑层次结构。 If you step from one dimension into the other, your logic may be over-extending. 如果您从一个维度转到另一个维度,则逻辑可能会过度扩展。 Maybe the logic could ask for what it needs, and a separate handler could join the handler to its data. 也许逻辑可以询问它需要什么,并且一个单独的处理程序可以将处理程序连接到其数据。

The point I'm trying to make is that I wouldn't intuitively look inside handleClick to find some event listener for when data comes back from the database. 我要说明的一点是,我不会直观地查看handleClick内部内容,以便为数据从数据库返回时找到一些事件侦听器。 Anything longer than like 50-100 lines is getting pretty savage long and taxing for someone to have to read through. 任何比50-100行长的时间都变得很野蛮,并且需要有人通读。 If you come back later and connect the top to the bottom, someone else might call it spaghetti, but they can't if you have one file per function per action per event (in which every function has one job and every object has one purpose). 如果您稍后再回到顶部并将其连接到底部,其他人可能会称其为意大利面条,但如果您每个事件每个动作的每个功能只有一个文件(其中每个功能只有一个工作,并且每个对象只有一个目的),他们就不会)。

I'm not saying it has to be similar to this. 我并不是说它必须与此相似。 I'm just saying modular is king because it makes it easy to extend, maintain, and refactor. 我只是说模块化是最重要的,因为它使扩展,维护和重构变得容易。 The less I need to absorb and front-load my short term memory when I see your code, the more I will enjoy it. 看到您的代码时,我吸收和提前加载短期内存的需求越少,就越会喜欢它。

Here is a sample directory tree that my opinion prefers, and I hope illustrates a way of thinking, not an exact solution: 这是我认为更喜欢的示例目录树,我希望可以举例说明一种思考方式,而不是确切的解决方案:

components/
    auth/
        auth_actions.js
        auth_types.js
        auth_reducer.js
        LoadingSpinner.js
        LoginForm.js
        SignupForm.js
        SplashScreen.js
queries/
    mongodb_checkIfEmailExists.js
    mongodb_getUserById.js
    neo4j_getUserByNodeId.js            

Now, imagine your friend is working with you. 现在,想象一下您的朋友正在与您一起工作。 You are doing work on the Login View and she is working on the Dashboard View. 您正在“登录”视图上工作,而她正在“仪表板”视图上工作。 You won't encounter any file collisions while working at the same time. 您不会在同时工作时遇到任何文件冲突。 You also probably have a pretty good idea what is going on as soon as you look at the files. 当您查看文件时,您可能也很清楚发生了什么。 You will also notice that all your imports are like ./Something rather than ../../../actions . 您还将注意到,所有导入内容都类似于./Something而不是../../../actions

I hope this is illustrative to the thinking style not the exact solution. 我希望这是对思维方式的说明,而不是确切的解决方案。

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

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