Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  115] [ 1]  / answers: 1 / hits: 20766  / 10 Years ago, mon, july 28, 2014, 12:00:00

I've got a jsbin for this issue here: http://jsbin.com/tekuluve/1/edit



In an onClick event I'm removing an element from the model, and re-rendering the app. But strangely, in componentWillReceiveProps() (and componentWillUpdate, and componentDidUpdate too), nextProps is always === to this.props, regardless of what I do.



/** @jsx React.DOM */
var Box = React.createClass({
render: function() {
return (
<div className=box onClick={ UpdateModel }>
{ this.props.label }
</div>
);
}
});

var Grid = React.createClass({
componentWillReceiveProps: function(nextProps) {
// WTF is going on here???
console.log(nextProps.boxes === this.props.boxes)
},
render: function() {
var boxes = _.map(this.props.boxes, function(d) {
return (<Box label={ d.number } />);
});

return (
<div className=grid>
{ boxes }
</div>
);
}
});

var model = [
{ number: 1 },
{ number: 2 },
{ number: 3 },
{ number: 4 },
{ number: 5 }
];

function UpdateModel() {
React.renderComponent(
<Grid boxes={ _.pull(model, _.sample(model)) } />,
document.body
);
}

React.renderComponent(
<Grid boxes={ model } />,
document.body
);


I need nextProps to be different to this.props after it has been updated via UpdateModel(), in the componentWillReceiveProps() lifecycle event.


More From » reactjs

 Answers
50

=== will check if it's the same object. It seems that what you're doing is mutating the value that the boxes property points to.


[#70021] Friday, July 25, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breap

Total Points: 606
Total Questions: 96
Total Answers: 108

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
breap questions
Thu, Jun 24, 21, 00:00, 3 Years ago
Wed, Mar 18, 20, 00:00, 4 Years ago
Mon, Oct 7, 19, 00:00, 5 Years ago
;