Skip to content

Commit 29609c6

Browse files
committed
Add getDerivedStateFromProps to support updating via store
1 parent 09f849f commit 29609c6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Rating.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ export class Rating extends React.Component {
1010
};
1111
}
1212

13+
static getDerivedStateFromProps = (nextProps, prevState) => {
14+
if (prevState.rating !== nextProps.value) {
15+
return {
16+
rating: nextProps.value,
17+
hoverAt: prevState.hoverAt
18+
};
19+
}
20+
return null;
21+
};
22+
1323
handleMouseOver(idx) {
1424
this.setState({
1525
hoverAt: idx + 1
@@ -32,6 +42,9 @@ export class Rating extends React.Component {
3242
const { onClick } = this.props;
3343

3444
return (<div>
45+
<p>
46+
Rating:
47+
</p>
3548
{
3649
[0, 1, 2, 3, 4].map(i => {
3750
const rating = this.state.hoverAt != null ? this.state.hoverAt : this.state.rating;
@@ -41,6 +54,7 @@ export class Rating extends React.Component {
4154
onClick={() => {
4255
this.handleClick(i);
4356
onClick({ value: i + 1 });
57+
4458
}}
4559
key={`${this.props.id}_${i}`}
4660
>

0 commit comments

Comments
 (0)