Change state of React component from old external Javascript?

How can I change the state of a React component from my old legacy jQuery soup

I have a component like this:

var AComponent = React.createClass({
  getInitialState: function() {
    return { ids: [] }
  render: function() {
  onButtonClick: function() {
    ids.splice(…); // remove the last id

When something special happens in the old jQuery soup code, I’d like to
push an id to AComponent.state.ids. How can I do that?

One “obvious” solution is an anti-pattern; here it is:

var componentInstance = AComtonent({});
React.renderComponent(componentInstance, document.getElementById(...));

// Somewhere else, in the jQuery soup. Something special happens:

This is an antipattern, according to this email from a Facebook
because he writes that componentInstance might be destroyed by React.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I would make the component stateless. Store the ids array outside of your component and pass it as a prop with functions that will modify the array. See example on JSFiddle:

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x