weixin_39803552的博客
- 于 2017-08-10 加入CSDN
-
获得3次点赞
-
内容获得0次评论
-
获得16次收藏
- 最近
- 文章
- 资源
- 问答
- 课程
- 帖子
- 收藏
- 关注/订阅




Confusing setState() documentation
I have a general question about setState()
because the documentation about this is a little bit confusing:
I have a component with a state. This state should only be updated (re-rendered) when a condition met. If not then there is no need to re-render the component. Reading the docs about setState tells me that reading this.state
right after calling setState()
is a potential pitfall. Instead I should use setState with an updater
function because there prevState
is guaranteed to has the right value. So my first approach was:
js
class MyComponent extends React.Component {
onSomething = () => {
this.setState(prevState => {
if (prevState.counter < 10) {
return { counter: prevState.counter + 1 };
}
return prevState;
});
}
}
But with that approach my component always get re-rendered because setState()
always forces a re-render (in the beginning I thought that when I return the same prevState
object React does nothing. But I was wrong). So what else can I do to avoid re-rendering?
Again in the same doc we can read that calling setState() only when the new state differs from the previous state will avoid unnecessary re-renders. So I could re-write my code into following:
js
class MyComponent extends React.Component {
onSomething = () => {
if (this.state.counter < 10) {
this.setState(prevState => {
return { counter: prevState.counter + 1 };
});
}
}
}
But then if (this.state.counter < 10)
could have a different value than prevState.counter
So on the one side the docs recommend to not read from this.state
directly because the calls to setState()
could be batched and this.state
has an outdated value but on the other side it recommends to call setState()
only when the new state differs from the old state. But how can I check if the state differs when it could be potentially outdated?
In the end I don't know actually how to set my state and avoid a re-rerender when a condition met. Any help here would be great.
该提问来源于开源项目:reactjs/reactjs.org

Keep fields order
Fields are returned in a random order after marshalling. I would not pay attention to it, if the return type was not OrderedDict
. Why use OrderedDict
, if the fields are still returned in random order?
I think the problem for this is the use of unordered set
here.
It would be great if the fields returned in the order in which they are declared in serializer. It is much prettier for RESTful APIs.
该提问来源于开源项目:marshmallow-code/marshmallow

Ok, just checked it. But it does not work with declaring serializer like so:
python
class PersonSerializer(Serializer):
id = fields.String()
created_at = fields.DateTime()
name = fields.String()
phone = fields.String()
email = fields.String()
The result looks like this:
json
{"name": "Guido van Rossum", "created_at": "2014-08-20T23:17:31.705290+00:00", "id": "125", "phone": "", "email": "a.com"}
I did not have much time to sort out what is the reason, but I think the problem in method get_declared_fields:
python
>>> s = PersonSerializer()
>>> s.declared_fields
OrderedDict([('name', <string field>), ('created_at', <datetime field>), ('id', <string field>), ('phone', <string field>), ('email', <string field>)])
</string></string></string></datetime></string>
