score:3
Accepted answer
When those class properties are transpiled, they get moved to the end of the constructor, in the order that they are written. So when you do this...
class Example {
state = {
hello: this.hello()
};
hello = () => {
return 'hello;
};
}
...it gets transpiled into roughly this:
class Example {
constructor() {
this.state = {
hello: this.hello()
};
this.hello = () => {
return 'hello;
};
}
}
In this transpiled code, you're trying to access this.hello before this.hello has been defined, and thus you get the error. Instead, you need to change the order, so that you define the function first, and use it after.
class Example {
hello = () => {
return 'hello;
};
state = {
hello: this.hello()
};
}
Source: stackoverflow.com
Related Query
- React - Arrow functions error (proposal class fields)
- Unable to use Arrow functions inside React component class
- What is the difference between arrow functions and regular functions inside React functional components (no longer using class components)?
- Babel Plugin Class Properties – React Arrow Functions
- Arrow functions in class throw error with webpack-encore
- Should I call a function on every render or use arrow functions in a react class component?
- error "Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`" in react jsx class
- How to Handle Error using try catch using React State with Arrow functions
- Correct use of arrow functions in React
- Can I use arrow functions instead of normal functions for React Hooks?
- When using React Is it preferable to use fat arrow functions or bind functions in constructor?
- Arrow functions as class properties using Babel
- React elements and fat arrow functions
- Getting error "Path" argument must be string while deploying React - Loadable components sample code in cloud functions
- Cannot call a class as a function, React router show error in console if i use es6 format
- ES6 functions, arrow functions and 'this' in an ES6 class
- How to test functions other than render in a React class using jest?
- How to test arrow function in React ES6 class
- Should one use const when declaring an arrow function in React class
- How do I test component methods on a React component that are defined as arrow functions (class properties)?
- Why does React throw an error when setState method used in a class constructor?
- React this.setState With Arrow Function Causes Error In Console
- Storing React Class functions in separate files
- Const = () => vs Class Functions function name() {} in React Native
- Change style of existing input fields whenever error occurs in React Formik
- React component function returning JSX causes error when used in render method of ES6 class React component
- connect react redux HOC got error of `Cannot call a class as a function`
- Should class methods be created with arrow functions
- React + ApolloProvider + Typescript = "JSX element class does not support attributes" error
- React HOC has a Typescript error as a class component but not as a function component
More Query from same tag
- file upload in react and node js not working
- React test on component's fetchdata returns "cannot read property 'catch' of undefined"
- Apollo 3 pagination with Field Policies
- How can I extract data from complex JSON file with reactJS?
- video.play() occurred unhandled rejection (notallowederror) on IOS
- How do I make sure my if funtion runs before my data is pushed to a database?
- How to render a component in a different page when click on a link in a table row in ReactJs
- ReactJS multiple select on Form.Control does not mark selected objects
- How To Change BorderWidth of Checkbox in Material UI v5
- Content editable is writing on the wrong direction when it has an value
- HTML rendering on server with Node.js
- React sorting doesn't work right on the first try
- Exposing React Components using Webpack
- no response from destroy method of cloudinary
- How emit event to LoadingOverlay component from Axios custom component
- Get Location on component mount and pass to App State in Ionic React
- First dynamically added component not getting styling from a class
- File browser is not opening after AJAX call in Firefox
- React - Passing information into a component, rendering as null
- React Native: How to export a method with a return value?
- i'm getting isssue while calling API in Useeffect in reactJs
- Why are these Jest tests failing? TypeError: Cannot read property 'apply' of undefined
- React map not a function
- Pass a wrapper component as props in ReactJS
- Disabling a button if an immutable list contains empty value
- Remove duplicates within the output of a reduce function
- React.js: Variable appearing as undefined on state change
- Conditional input placeholder with special characters (HEX value)
- Axios catch response in react when calling Laravel API
- Formik , how to reset dirty property