Looks like functions which are part of the React framework are referred to as "methods":
"The only method you must define in a React.Component subclass is called render()."
https://reactjs.org/docs/react-component.html
However, this excerpt is from further down in the same webpage:
"The render() function should be pure, meaning that it does not modify component state"
So even in the same webpage on the official React website, render() seems to be referred to as either a function or a method. Do you use any guidelines for using the term "function" vs "method" when discussing these constructs in React? Or in the React world are these two terms completely interchangeable with no real discernment or nuance?
Do you use any guidelines for using the term "function" vs "method" when discussing these constructs in React?
No guidelines between referring to them either way because both terms are correct. However, I tend to hear render function more instead of render method from my experience.
It's most preferable for using the term "method" when explicitly mentioning/referring to "Class" or "Object" as in the case of the sentence the React docs used:
The only method you must define in a React.Component subclass is called render()
Still, it's up to preference regardless.
For reference on what a method is from MDN:
A method is a function which is a property of an object
https://developer.mozilla.org/en-US/docs/Glossary/Method
Generally the term "method" is used when describing a function that belongs to an object or class.
To use the term function is always correct, the reason react often uses the term "method" is because in react the code is often running inside of a class.
See What's the difference between a method and a function?.
In React, we usually talk about "method"s when we want to refer to a function in class component, like lifecycle methods.
Otherwise, it's just a normal function (arrow function included).
Don't get confused between the terms function and function component as they are not the same.
Seems like they're the same thing. Here's my conjecture for why people use both:
In class-based object oriented programming, the term "function" doesn't exist. Only "method". a public method, a private method, getter vs setter methods, etc.
In Javascript, the same concept has always been known as "function".
Javascript isn't originally a class-based object oriented programming language, although there are ways to make it that way. React, makes JS a class-based OOP.
So people who use the term "function" adopt a language that used to have "functions" but is now suddenly a class based object-oriented language in which you're supposed to have "methods", and the writers of the docs aren't opinionated and meticulous enough to set a standard, so they use both terms interchangeably and it leads to this confusion.
Related
I'm trying to figure out what is the purpose of introducing something called custom hooks when its just a function. If I take the following example https://medium.com/from-the-scratch/react-custom-hooks-a-simple-intuition-if-you-still-cant-hit-it-off-8d27fa4ba10, if I don't use the use prefix for the hook, it all still works fine. With the introduction of this terminology called custom hooks I'm not sure whats the purpose of it, or should I just go on using standard functions.
What is the main advantage I get when using the use prefix for a custom hook or function apart from some simple linting features?
What is the main advantage I get when using the use prefix for a
custom hook or function apart from some simple linting features?
Reasons are stated in docs:
Do I have to name my custom Hooks starting with “use”? Please do. This
convention is very important. Without it, we wouldn’t be able to
automatically check for violations of rules of Hooks because we
couldn’t tell if a certain function contains calls to Hooks inside of
it.
In very basic words: You can reuse stateful logic between components, witch is not possible with regular functions. I would suggest that you read the documentation, because ReactJS has a pretty good one :)
Stay safe
Nothing. There is no difference between them. useFoo function is just a function. In source code, it was not dealt with specially. It is just little bit complicated, but no difference between regular functions.
use prefix is just a term, or just culture, whether you obey it is entrusted to you. Of course, obeying the culture bring us good result. But this and that have no connection. Custom hooks are just functions.
I am beginner to spring aop and i am going through spring aop documentation to understand the concepts but failed to understand 'target object'.
the documentation says target object is the "object being advised by one or more aspects. Also referred to as the advised object".
what is the meaning of being advised by one or more aspects here? can anyone explain me what is target object in Lyman terms as i am still a beginner.
For a simple explanation of some basic AOP terms please refer to my other answer. Please read that one first before continuing to read here.
So the target object is the (Java or Spring) component to which you want to add new behaviour, usually a cross-cutting concern, i.e. some behaviour that is to be applied to many classes in your code base.
An aspect is a class in which you implement that cross-cutting concern and also determine where and how to apply it. The where is defined by a pointcut, some kind of search expression finding the relevant parts of your code base to apply the behaviour to. The how is implemented in an aspect method called an advice.
So when we say that an aspect advises an object, it means that it adds (cross-cutting) behaviour to it without changing the class itself.
In Spring AOP this is mostly method interception, i.e. doing something before or after a method executes.
In the more powerful AspectJ you can also intercept changes of member variables and constructor execution. Furthermore you can change the class structure itself by adding new members or methods or making the target class implement an interface etc.
Is it possible to define multiple targets like below:
#Before(value = "com.test.createUpdateDeletePointCut() && (target(com.testlab.A) || target(com.testlab.B))")
Recently I've been seeing a lot of examples in blogs where the methods inside React functional components are given an underscore. I also saw this in class-based components and was told it meant they were private (?).
But functional component internal functions are already private and inaccessible outside of the component, right?
Example:
function MyComponent({ propOne }) {
const _getData() {
/// Why does this have an _underscore on it?
}
return (
<div>a div</div>
)
}
It's a naming convention that the Airbnb React/JSX Style Guide (version 2019.05.24) actually warns against:
Do not use underscore prefix for internal methods of a React
component.
Why? Underscore prefixes are sometimes used as a convention in other languages to denote privacy. But, unlike those languages, there is no native support for privacy in JavaScript, everything is public. Regardless of your intentions, adding underscore prefixes to your properties does not actually make them private, and any property (underscore-prefixed or not) should be treated as being public. See issues #1024, and #490 for a more in-depth discussion.
In short:
The underscore to denote private methods is borrowed from other languages.
It doesn't change the method itself.
It's to show that the method is meant to be private and prevent its use.
It's up to you whether to follow the convention or not. There's no need to. If you follow the style guide cited above, you shouldn't. However, it also depends on the people you work for / with, e. g. if the company uses a style guide with the leading underscore to denote private properties.
Example for this convention in another language - Python. From the Naming Convention:
_single_leading_underscore
This convention is used for declaring private variables, functions, methods and classes.
_functionName doesn't mean private, it is just a best practice.
If are interested in writing custom hooks react document suggest to prefix "use" before hook name ex: useCustomeHook https://reactjs.org/docs/hooks-custom.html
Functional component is a tiny function which accepts only props and return HTML in ReactJs.
Just as Jeanne Dark said, it's just a naming convention. It's up to you of course how to type your code, but I would not recommend using the underscore prefix, as you can see for yourself that the function is private without the underscore.
You can read about JS naming conventions here on W3 schools, or maybe even more trustworthy, googles JavaScript style guide.
New to ReactJS. Got this on a "join reactJS event questionary" but was unable to compile it. It seems to be missing a React Component class definition for the Item and List.
Is this shorthand style valid?
Is this shorthand style valid?
yes, it is valid code, List and Items are Stateless functional components
In order to run this code you need use babel with babel-preset-react
Yes, they are stateless functions, aka "pure components". They take their props as their only parameter and return the render results. If you do not need to keep track of any state, they are very lightweight both to understand mentally and in terms of the resources needed by React to manage them.
As to why you were unable to compile it, possibly you do not enable the right ES6/ES2015 features in whichever compiler you're using. You are using among other things super calls, object destructuring (in the parameters) and arrow functions. For help with this, provide the specific error message.
It is a valid javascript es6 code, except that multiple dynamic children (here the list of Items) need to have key prop https://facebook.github.io/react/docs/multiple-components.html#dynamic-children
I am newbie in Elixir, coming from java background. I saw Elixir's function as static methods in java. So I wonder, is there any non-static method / function in Elixir?
Thank you
Nope - all functions belong to a module. Elixir is not an class-oriented language, so the concept of "instance methods vs. class methods" is not applicable.
Aside from typical named functions which belong to a module, there are anonymous functions, similar to lambdas in Java.
The accepted answer is correct and I upvoted it. The basic building blocks in OOP are objects. On the BEAM (Erlang VM), the basic building blocks are processes. So, the distinction between static/instance methods just doesn't make sense.
However, when thinking about what instance methods do in an object oriented language, there is something that does a similar thing in Elixir.
Instance methods, when contrasted with class methods, are the ones that work with internal object state. Elixir doesn't have classes or objects, but it does have processes. A GenServer process instance maintains state and passes it into each callback function. So, when you're looking for something that will have state and functions to modify it or return some piece of it, then you want to reach for a GenServer in Elixir.
All the functions will still belong to the Module. They aren't a unique type of function, but they do allow you to manipulate the state of a given instance of the process because the state gets passed in as a parameter and returned within the function's result.
In response to the comment by #ibgib, yes, when compared with an object oriented language like Java or C#, you can think of all modules and functions in Elixir/Erlang as being static. This is comparing apples to oranges, but if it helps when learning to think of them that way, I think that's OK. Just realize that there isn't any such thing as instance methods here.