score:2
If you use the first approach which is defining the method inside the constructor like this
constructor() {
this.toggleTheme = () => {
this.setState(state => ({
theme:
state.theme === themes.dark
? themes.light
: themes.dark,
}));
};
}
Then when your component usesthis.toggleTheme
as a callback, you don't have to bind its this
reference to the current component in which it is defined, e.g. this.toggleTheme = this.toggleTheme.bind(this)
, on the other hand, if you define toggleTheme
as a method outside the constructor as in your second example, and if toggleTheme
is passed as a callback, you will get "setState is not defined" or something like that when toggleTheme
is invoked
Also, with the first approach toggleTheme
is added as a instance property to the component class meaning each component instance will have a separate copy of toggleTheme
, whereas the second approach will add it to the prototype of the component class which is better in terms of memory consumption because all component instances will share that method on the prototype
score:0
The difference between this two approaches:
class MyComponenet extends React.Component {
constructor(props) {
super(props);
this.method = () => {
console.log('Method');
}
}
render() {
return null;
}
}
... and ...
class MyComponenet extends React.Component {
method() {
console.log('Method');
}
render() {
return null;
}
}
Is that the first approach defines the method
with the arrow notation which automatically binds the function's this
to be the instance of the component class while the other doesn't.
You could change the second example to:
class MyComponenet extends React.Component {
method = () => {
console.log('Method');
}
render() {
return null;
}
}
This would be the same as the first example, but keep in mind you have to enable your transpiler option that allows this syntax.
Source: stackoverflow.com
Related Query
- React 16.3 class method vs constructor method
- What will happen if I use setState() function in constructor of a Class in ReactJS or React Native?
- componentDidMount method not triggered when using inherited ES6 react class
- "Missing return type on function" for every React class method
- React hooks: How to write variables in functional components that in class components were initialized in the constructor
- TypeError: Class extends value undefined is not a constructor or null in react js
- How to provide dynamic className to element in React Class render method
- How do i call a method within another method in react class Es6
- React 0.13 class method undefined
- React binding this to a class method
- Why does React throw an error when setState method used in a class constructor?
- react class method is not defined no-undef
- Constructor Method in React
- What's difference between two ways of defining method on React Class in ES6
- React component function returning JSX causes error when used in render method of ES6 class React component
- React TypeError: Class constructor Fullpage cannot be invoked without 'new'
- Is it possible to safely destructure an ES6 React Component class in its constructor signature?
- How do I correctly spyOn a react component's method via the class prototype or the enzyme wrapper instance?
- Jest testing that a React class method was called by componentWillMount
- React class method not defined no-undef
- Call a method into the class React ES6
- React/ES6 -> How to call an React component class method from another component?
- How can I convert a class with a constructor to a functional component using React Hooks?
- Function outside React class method undefined
- react difference between class method and file global function?
- React Class method returns undef instead of a verified value
- How do I access a React Class method from outside?
- Call child method from parent class - React typescript
- how to implement a class method on a function or a function constructor method on a class?
- React - Creating table using loop in class method
More Query from same tag
- React Ref undefined even after assigning it
- React ajax call when button onClick event using hooks
- Is there a better way to use conditionals inside jsx?
- How to get the return value of a async function that returns a promise
- How can i keep my changes after not Intersecting to my element?
- React, pass value from if statement to const with template literals
- Formik `isSubmitting` and `isValidating` are `false` during pre-submit validation
- Why is useState within useEffect not working in React?
- React - Invariant Violation: Maximum update depth exceeded
- Why am I getting a 'no-unused-var' error when using in an inline function
- Fetch API for Django POST requests
- React app slows to a crawl with chrome developer tools open. Works fine in incognito
- ReactJs - Call Second Function Parameters
- How to trigger animations in a React app based on the scroll position
- How to Implement Infinite Scroll in Next JS SSG?
- Using nested object names in formik/<ErrorMessage/>
- How to transfer HTML datetime picker datetime-local to moment format
- Gatsby markdown images are stuck with blurred span
- setState doesnot change the state in React
- Using .map on a component causes component to return "Nothing was returned from render...." error
- React.js, rerender Child Component by onChange event
- User or pass wrong message in service api
- Cypress wait for API after button click
- TypeError: this.addScope is not a function while trying to React + Firebase Googel Auth
- componentDidUpdate called twice every route change
- Configure React Dev Server within an ASP.NET Core Application
- Setting a default value for a select when select is based on API call
- How come this.setState doesn't work properly in componentDidUpdate hook. Feels like async issue, but don't know how to handle it
- Include regular JS file in a react project
- Gauge Series of Highcharts with React not the Solid Gauge but Gauge Series