score:3
use one of these:
this.finalize = this.finalize.bind(this);
in constructoronchange={this.finalize.bind(this)}
onchange={e => this.finalize(e)}
finalize = () => {};
you can read more about the pros and cons here.
score:1
it looks like you've identified the issue. when the finalize()
method is called, this
is bound to the window.
you can solve the issue by binding the function in the constructor. for example:
constructor(props) {
super(props);
this.state = {
optionlist: []
};
this.finalize = this.finalize.bind(this);
}
it's generally better to bind in the constructor than to bind in the render
method, because if you call bind
in the render method, you are generating a new function each time.
for more information, take a look at this: https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56
Source: stackoverflow.com
Related Query
- How to access a public method from a wrapped Component using TypeScript?
- How to access component method in React from a WebSocket callback
- how to access "this" from component method
- How to access method of the child component from parent in reactjs
- React functional component - How to access children method from Parent
- How to access the parent's component method from its nested component in ReactJS
- How to access component methods from “outside” in ReactJS?
- How to access the "key" property from a reactjs component
- How to access one component's state from another component
- React, how to access the DOM element in my render function from the same component
- How do I call a method from another class component in React.js
- How To Declare TypeScript Static Method From React Functional Component
- How to Access EditorState from Decorated Component
- How to access a static function from a component instance in react?
- React: How to access component refs from Redux/Flux actions?
- Access a React component method / state from outside the component
- How to access route params from a component other then the Router component
- How to get access to CSS values from a styled component (React)?
- How to create a sortable table in React? How to access class method from the sorted objects?
- How can I call a component's method from another component in React?
- How to make React.js fetch data from api as state and pass this state data from its parent to child component
- How to call child component method from parent component in react?
- How can I access child components values from a parent component when they are added dynamically?
- How to access a Wrapped Component state from HOC
- How can I access a class component method in a function component?
- How to call a component method from another component?
- How to pass function from FUNCTIONAL to CLASS component and access it outside of render( without prop ), using context in react js?
- React: how to access component function from a click event on vis.js edge?
- React/ES6 -> How to call an React component class method from another component?
- How to access previous index route in nested routes using Link component from react router?
More Query from same tag
- Background images for dynamically rendered components not appearing on first render
- Can't load mock data while mounting component
- Spring Boot with Webflux: No 'Access-Control-Allow-Origin' header is present on the requested resource
- proper way to get entity indicator id from end of route in react?
- Is setState() inside componentDidMount() considered an anti-pattern
- (React) How to match the modal content with the clicked element?
- Antd table cell render charts
- add custom object by modifying the object by matching the json object using javascript / node js
- What is the difference between ampersand and double ampersand in Less?
- Separate query from body in Axios request
- React.js: Parent state values not passing into child properties + Fetch API data cannot be accessed
- Skip or remove newline within pre and code tags in string
- Selection Checkbox in React using Hooks
- Why is this.props undefined when I'm passing in actions to mapDispatchToProps?
- Is there any way I can apply different inline style when I mapping through array of objects inside jsx?
- Learning React proof of concept
- Closing the modal programmatically (react-materialize)
- Replace multiple strings within string with react jsx element
- Why does each state hook update cause a separate re-render in asynchronous code?
- React key warning on listing component, coming from the parent component
- How to update whole Uint8Array value using useState in React Hook?
- How come this.setState doesn't work properly in componentDidUpdate hook. Feels like async issue, but don't know how to handle it
- Objects nests when bringing the object to another file
- create react app and SVG from node modules folder
- React Render UI Before Redirect
- babel watch SyntaxError: Unexpected token
- How to provide background color to x% width of Ag Grid Cell?
- TypeScript + React + Emotion: typesafe ref forwarding when using `as`
- Add a video to a ReactJS app and make it visible on the view
- React Slick not working