score:2
Accepted answer
Obviously you can't have two different this
value.
The simplest solution would be to capture the outer context in a closure
const component = this
this.db.run(
'INSERT INTO table...',
{},
function (error) {
console.log(this.lastId) // is available
// check error
if(error !== null) {
component.props.addNotification({
open: true,
variant: 'error',
message: error.message,
autoClose: null,
});
} else {
component.props.route.history.push('/...');
component.props.closeForm();
}
}
);
or you could create a temp normal function that would pass this.lastId
to your callback as the second argument
this.db.run(
'INSERT INTO table...',
{},
(cb => function(){return cb(error, this.lastId)})((error, lastId) => {
console.log(lastId) // is available
// check error
if(error !== null) {
this.props.addNotification({
open: true,
variant: 'error',
message: error.message,
autoClose: null,
});
} else {
this.props.route.history.push('/...');
this.props.closeForm();
}
})
)
score:0
You can do following:
// above function
const self= this;
// inside function
function(error) {
this.lastId;
self.props.history;
self.state;
}
Source: stackoverflow.com
Related Query
- Is there a way to get "this.lastID" from sqlite.run() callback using es6 and react classes
- Is there a way to get the coordinates from this draggable marker using google-map-react?
- Cannot get rid of this comma on the bottom of html. I am using react and in the index.html I don't see it there or in any of my components
- Is there a way to map over the cities on my cities array using the api and rendering the info from all cities on the array?
- Anyone can fetch my blog posts using my GET end-points and use them on his own site? Is there a way to protect this?
- Is there a way to get all this datas out and have them in an array
- Is there a way to unit test an innaccessible callback function that is called from a child React component using jest
- Is there a way to get all data from a firestore database using useCollectionData()?
- is there any way to get the location name from the longitude and latitude positions in react js google map
- Is there a way to map over an array of objects in react and also get the accosiated images from cloudinary
- Is there any way to fetch only city and country from user input on googleplacesautocomplete api using react?
- Why is my axios get call repeating over and over using React.useEffect to fetch from Rails backend?
- Is there a way to mock `process.env.NODE_ENV` in my unit test using webpack and mocha?
- How to get data from local json file using actions and axios.get() (Redux)?
- Using a hook in react to change boolean and get a callback
- Ist there a way to pass data from child to parent using Hooks?
- Is there a better(cleaner) way to write this JS code using ternary operators(without repeating code)?
- Is there way to get return value from reducer action? (in React-Native)
- Is there any way to get current user from the request? I am getting annonymous user even though user is loggedin
- How can i get array of object from this data for state and count in reactjs
- How to retrieve data from the server using fetch get method and show it in a table
- Faced this error while using React-Hook-Form and YupResolver: Attempted import error: 'set' is not exported from 'react-hook-form' (imported as 'o')
- How to reduce and get value from object array using javascript
- Are there any functional differences between using fat arrow syntax or not for ES6 class methods and React?
- JSON missing a template for this request format and variant coming from React using Stripe
- Get object array from axios and using return value call another axios function and append the output to first result
- Is there a way to upload a file to Node.js using React and Observables (rx.js)?
- Is there any way to speed up PDF download on mobile browsers using a combination of JSPDF and html2canvas?
- dynamic setState and get state using es6 in react
- I have a question about Cloud Firestore V9 and using React Js. In one of my docs, there is a map. How do I access this map?
More Query from same tag
- How to fire a function in child from parent in react?
- Styling rendered iframe in react
- CORS Policy blocking my API requests in a google cloud platform app engine in a live website
- Using debounce with React hooks causes endless requests
- Axios is returning undefined
- Reveal icons on scroll in React.js
- How to dynamically order my react components according to the order I get from my request?
- Button onClick does not working in reactjs
- How to map list of objects
- redux-react-session how to get user info into state on refresh
- How to fix background color unit test
- Does "webpack-dev-server"' compile my files to disk?
- How to reduce the bar width in chart js doughnut chart
- Pre-render data into SPA application
- React default props for array property in required object
- Can't figure out why my code won't render
- Is there a better method to test individual methods of a class
- Nodejs and React inside docker containers
- How to pass props on Pseudo-elements with styled components?
- Update other component on input value change React
- What is the simplest way to pass state while using React Router?
- Pass an Image imported to react Component via props
- Issues with Modals React Native
- how to add type annotation for mock function
- How to give round corners to the TableHead of Material UI
- How to return what's being entered in input - JavaScript/React
- couldn't get to detail page for each restaurant from my Restaurant list
- With React-Select, how can I make the dropdown accessible through a React-Bootstrap-Glyphicon?
- react hook form: materail ui: Textfield: onSubmit, not passing Filelist in the data
- How to deploy ReactJS web application on a windows server using Node.JS?