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.

reactjs docs reference

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.


Related Query

More Query from same tag