Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use setProps in react.js

Tags:

reactjs

I want to call setProps from outside of myComponent to be able to dynamically change data for myComponent.
I expect that after changing props of the component, it will re-render itself.

I was trying the following:

var myComponent = React.createClass({
  render: function () {
    return (
      React.DOM.div(null, this.props.data)
    );
  }
});

React.renderComponent(
  myComponent({ data: someData }),
  document.getElementById('predictionContent')
);

myComponent.setProps({data: someData2});

The problem is that I don't understand how to use setProps for the component. In my case, I receive "undefined" error.

How to solve this?

like image 778
31415926 Avatar asked Aug 05 '14 12:08

31415926


People also ask

How do you pass data through props in React?

In order to pass a prop to a component all we have to do is name the prop and set it equal to some value. In the example above, we are passing a prop called name that is equal to a string. Passing a prop gives us access to the information in our Greeting component.

What is setProps?

The set props are the large movable items not built into the set. Generally this is the furniture or “sittables” and would include things like chairs, tables, rugs, appliances, barrels, trunks, or large rocks. But it can also include large items like tents, a canoe, a car or even a wrestling ring.

How render data in react JS?

The ReactDOM. render() function takes two arguments, HTML code and an HTML element. The purpose of the function is to display the specified HTML code inside the specified HTML element.


2 Answers

warning: setProps is now deprecated.

React.createClass returns a class, it is React.renderComponent which returns an instance of the class which has the setProps method.

Try this instead:

var MyComponent = React.createClass({
    render: function () {
        return React.DOM.div(null, this.props.data);
    }
});

var myComponent = React.renderComponent(
     new MyComponent({ data: someData }),
     document.getElementById('predictionContent')
);

myComponent.setProps({ data: someData2 });

Having said that, Chad Scira's answer is also correct: probably better to re-render than to call setProps. That will keep it looking the same as the code inside the render() method, and you'll always be able to call renderComponent, regardless of whether it is the first time or a subsequent update.

Like this:

var MyComponent = React.createClass({
    render: function () {
        return React.DOM.div(null, this.props.data);
    }
});

React.renderComponent(
     new MyComponent({ data: someData }),
     document.getElementById('predictionContent')
);

// later
React.renderComponent(
     new MyComponent({ data: someData2 }),
     document.getElementById('predictionContent')
);
like image 159
Douglas Avatar answered Oct 20 '22 00:10

Douglas


You're not supposed to do that. Instead just run the renderComponent method again like this:

React.renderComponent(
     myComponent({ data: someData2 }),
     document.getElementById('predictionContent')
);

react will automatically resolve the differences

like image 30
Chad Scira Avatar answered Oct 20 '22 02:10

Chad Scira