简体   繁体   English

React.js:在React.createClass块内部和外部调用方法

[英]Reactjs: Calling methods inside and outside of React.createClass block

Warning -This will sound like the most noob question ever, but I think it is a confusing thing for people trying to process React components initially (or maybe I'm an idiot haha), but I thought this would be a good question for the beginner community. 警告-这听起来像是有史以来最菜鸟般的问题,但是对于那些最初尝试处理React组件的人(或者我是个白痴哈哈),我认为这是一个令人困惑的事情,但是我认为这对于初学者社区。

So I wanted to demystify a common thing that happens whenever I create a new helper method. 因此,我想揭开创建新的辅助方法时发生的普遍现象的神秘性。 I always wonder -should I be putting this inside "React.createClass" block or put it outside of "React.createClass" block. 我一直想知道-应该将其放置在“ React.createClass”块中还是将其放置在“ React.createClass”块之外。

Its obvious when it is a method that is part of the component life cycle or a render method, but I've seen examples helper methods put both inside and outside and it's hard to tell if there is a definitive line(guidelines) for when it is proper to do one or the other. 当它是组件生命周期中的一种方法或render方法时,这一点很明显, 但是我已经看到了一些示例性的帮助器方法放在内部和外部,并且很难确定何时有明确的路线(准则)做一个或另一个是适当的。

Are there guideline principles or cases for a React developer to know which 是否有指导原则或案例供React开发人员了解
case it better? 更好吗?

Generally you will want to keep your code blocks small enough to see clearly what is happening with that block. 通常,您将需要使代码块保持足够小,以清楚地看到该代码块正在发生什么。 So if you have a long helper method then outside is probably better. 因此,如果您有很长的助手方法,那么外面可能会更好。 If you have a lot of helper methods then you may want all of them outside. 如果您有很多辅助方法,那么您可能希望将其全部放在外面。 The basic rule is for your code to be clear and understandable. 基本规则是您的代码必须清晰易懂。

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

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