score:1
It has to do with the scope of this
when you use an arrow function this
will be scoped to the component class and not the function itself. So if you want to use a normal function you will need to bind the function in your react components constructor like this.fetchSearchImagesBound = this.fetchSearchImages.bind(this)
score:0
you are setStateing the state variable, you need to bind that method(this.fetchSearchImages.bind(this)) or use arrow function
score:0
In your first code:
fetchSearchImages = () => {
const { searchStart, count, term, searchImages } = this.state;
this.setState({
searchStart: searchStart + count
});
Here "this" refers to this of function fetchSearchImages, not your component's this.
When you do this:
fetchSearchImages = () => {
const { searchStart, count, term, searchImages } = this.state;
this.setState({
searchStart: searchStart + count
});
HEre "this" refers to "this" of your component because arrow function does not have it's own "this". That's why you have to use arrow function to access "this"
Source: stackoverflow.com
Related Query
- State destructuring only working in arrow function class method?
- Throttle not working when calling class method from inside throttled function
- Arrow function not working in react class component
- Invalid hook call. Hooks can only be called inside of the body of a function component when apply style to class base component with material-ui
- How to spy on a class property arrow function using Jest
- How to test arrow function in React ES6 class
- Should one use const when declaring an arrow function in React class
- Can't see state inside method helper function using hooks
- .simulate('click') only working when done twice on the same component, through ID and Class
- React component function returning JSX causes error when used in render method of ES6 class React component
- Lodash 'get' not working when use in a Arrow Function (React)
- Using Arrow function for class properties in React. Not clear
- How do I pass a React Context's state through an onClick function created in a method inside that context class?
- es6 arrow function in class property
- React: using state in a passed callback function to a class constructor doesnt use latest version of state
- useState hook's set State function not working properly with socket.io
- How can i reusable function with boolean state in class component
- React Hooks state not working properly when the function calling it is called in another function
- Arrow function "this" binding not working in React component
- Should I call a function on every render or use arrow functions in a react class component?
- How can I access a class component method in a function component?
- How parameter is working in single line expression of arrow function in useState function
- set state in a function not working - reactJs
- Javascript Syntax - Arrow function for class
- ReactJS - setting state in an arrow function
- Pass state from class component to function component in react
- How to call method in child component only once after parent state update
- Function outside React class method undefined
- Get updated state of component when calling method on class via enzyme
- class level variable updated inside a componentWillMount function not accessible in render method after a function call
More Query from same tag
- reactjs - Uncaught TypeError: Cannot read property 'map' of undefined
- Module not found: Error: Package path . is not exported from package
- How to pass React click events to electron
- Timeouts in Reactjs?
- Understanding synthetic events in ReactJS
- react native doesn't fill up window on ipad
- How to format number correctly in a calculator app (React)?
- react.js call parent function from child
- How to ensure I am reading the most recent version of state?
- npx create-react-app running with old version
- Inserting <br /> in ReactJS iteration
- What is the best way to call inherited method (through 'props') on ReactJS?
- How to make react semantic UI TextArea with default value, but still allows user to change the value?
- Return Object Key based on Value
- Passing props from react router to children on the server
- Could not type any text inside textfield
- Day picker - disable days before today not working
- React Web App Not Working or Deploying Correctly Now
- Is there a way not to re-render some part of a React state?
- Selective state not updating in React component
- React Passing Data to Stateful Component from Another Component
- how to encrypt/decrypt with crypto-js
- How to export a variable (its a list actually) to other .js file in a function inside class
- Getting simple styles to work with react arrow functions
- React Context API - persist data on page refresh
- Using Aladin Lite app (not built for React) in React app
- Next.js getServerSideProps redirection ERR_HTTP_HEADERS_SENT error
- How do I pass data to a Fauna Create function
- How to handle authentication from a Node Express redirect to React Client (react-router-dom and useContext)
- Storing image in base64 format in MERN stack