score:0
you can use jquery.proxy()
to use a particular context.
$.get('https://api.rss2json.com/v1/api.json', data, $.proxy(function(response) {
if (response.status == 'ok') {
console.log(response.items);
this.posts = response.items; // undefined
}
}, this));
score:0
you can try this
fetchposts = () => {
var self=this;
var data = {
rss_url: 'https://medium.com/feed/tradecraft-traction'
};
$.get('https://api.rss2json.com/v1/api.json', data, (response) => {
if (response.status == 'ok') {
console.log(response.items);
self.setstate({posts:response.items});
}
});
}
score:0
you should try for callback -
var callback = function(value)
{
this.posts = value; //check here weather this is accessible or not otherwise use var self = this
}
$.get('https://api.rss2json.com/v1/api.json', data, (response,callback) => {
if (response.status == 'ok') {
console.log(response.items);
this.posts = response.items; // undefined
callback(response.items);
}
});
score:1
basically, you should keep your posts
in state.
when value is changes over the time it should be in the state
(if it's not coming from the parent component) and then you just need update it.
so, your code will look like:
class mediumposts extends component {
constructor(props) {
super(props);
// init state
this.state = {
posts: [],
}
}
fetchposts = () => {
// because you are pass arrow function in the callback we have to save `this`.
// or you can create callback function in the component and pass it
const self = this;
const data = {
rss_url: 'https://medium.com/feed/tradecraft-traction'
};
$.get('https://api.rss2json.com/v1/api.json', data, response => {
if (response.status == 'ok') {
self.setstate({posts: response.items});
}
});
}
componentdidmount() {
this.fetchposts();
}
render() {
return (
<ul>
{this.state.posts.map(function(item, i) {
return (
<li key={item.guid}>
<h4>{item.title}</h4>
<time>{item.pubdate}</time>
</li>
)
})}
</ul>
)
}
}
export default mediumposts;
anyway, i advice you to get rid of jquery
in reactjs projects. instead, use axios.
hope it will helps.
Source: stackoverflow.com
Related Query
- How to save jQuery Get response to React variable?
- How to save axios response to a variable and update the state in react js?
- How to get iOS 12 Autofill to ask to save password in React Native app?
- How can I get a variable from the path in react router?
- React TypeScript: How to get form values, and response codes using fetch
- React how can you pass a variable to a get request
- how to get a Component variable from another component but their both in the same file React
- How to get Image response from react canvas draw?
- How to get React to listen for a click event called in Jquery
- How to get ThemeContext state as variable in Javascript inside react component?
- How to security save Facebook response token to React app?
- How to save api response in usestate and pass it react player source
- how to get response data in fetch if status code is 400 react js
- How can I get the jsessionid from a response using react js?
- How to get the response data from backend to React in React-dropzone-uploader
- How to save axios GET response data as json or as same as the arrived response
- How to post request login to get into dashboard . and save the token in react js
- How to use an axios response in another axios get in React JS?
- How to get HTML tag from API response in React Js?
- How can i store api response in variable and pass it to react popup video player
- How to save response data in a variable or state in js
- How to get a response from express server about an error using axios in react app
- How do I get the HTTP response code from a successful React query?
- How to save a class inside a variable with setState with React
- Get variable from window in React app from jQuery file
- How to save an id in a variable and show it in console in react
- How to use png response in axios get request in react
- How to store response data from Axios to a variable in React Functional Component?
- How to read a react file ( ES6 with jsx) with node in order to get the export variable
- how to get this react ajax response and set state
More Query from same tag
- How to dynamically update query parameters to URL string in react and axios
- How can i position this item so that it is attached to the left?
- Dynamic import with React and create-react-app
- Map without key on non markup return
- Othello Minimax Algorithm Doesn't Work In React.js
- Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined
- React TypeScript accepts missing props on ComponentType prop
- React MUI partially collapsible grid sections
- Setting styles for react-slick
- Next.js keeps current page active and re-rendering while getInitialProps of next page finishes loading
- Fetch data before component render, Reactjs
- How to handle loading data using fetch and React Hooks
- Next.js sitemap with url paramters
- React + SPFx I get my empty list and only get the items by manipulating them
- Putting Card in Center of The Page in React
- Error while trying to access files in the app
- Fast Refresh + Update function implementation of object that is in React state
- Reducer in redux is not activating
- Select Next/Previous list item in React without re-rendering
- Return value from child component to parent component React
- react-mic record audio and send the blob as mp3/wav to backend
- React with Google Maps Api, how to recenter map
- Limiting Post-Map Results
- Failure to delete from Firestore with React
- Moment returns wrong datetime
- How to make a POST request using jquery?
- Display image stored as a Path string in the Database in React
- Understanding ReactElement proptype
- Flexbox to fill available space not working inside React.js component
- ReactJS component not re-rending after setState update?