ReactJS中this.state和this.setstate的区别是什么? [英] What the difference of this.state and this.setstate in ReactJS?
问题描述
我想更改 hasSubmit
键的值,就像在First Code部分中一样。我知道这不推荐。但第二个代码是异步的,我不想使用 setState
的回调函数。
- 有什么区别
this.state
和setState
? - 有没有办法立即更改状态值
hasSubmit
?
第一个代码:
this.state.hasSubmit = false
this.setState({})
//代码将使用`hasSubmit`。
第二代码:
this.setState({
hasSubmit:false,
});
//将使用`hasSubmit`的代码。
ADD:
情况是:
- 当我点击
hasSubmit
在getInitialState()
中设置false
。
hasSubmit
将更改为false
>提交按钮。
hasSubmit
将更改为true
提交时。
首先点击提交
没有问题, hasSubmit
将设置为 true
。
但是第二次点击 submit
使用第二个异步代码
会出错,因为 hasSubmit
仍然是 true
,而 First Code
可以解决问题。
这是 React docs 说:
从不变异
this.state
直接,因为之后调用setState()可能会替换你所做的突变。将this.state视为不可变。
setState()
不会立即改变this.state但会创建待定状态转换。调用此方法后访问this.state
可能会返回现有值。
无法保证同步操作调用setState和调用可以进行批处理以获得性能提升。
setState()
将始终触发重新渲染,除非条件渲染逻辑在中实现,shouldComponentUpdate()
。
如果正在使用可变对象并且逻辑无法在
shouldComponentUpdate()
中实现,则调用setState()
仅当新状态与先前状态不同时才会避免不必要的重新渲染。
以设计方式使用API总是明智的。如果文档说不改变你的状态,那么你最好不要改变你的状态。
同时 setState()
可能在技术上是异步的,它肯定不会以任何明显的方式缓慢。组件的 render()
函数将以非常短的顺序调用。
直接设置状态的一个缺点是React的生命周期方法 - shouldComponentUpdate()
, componentWillUpdate()
, componentDidUpdate()
- 依赖于使用 setState()
调用的状态转换。如果直接更改状态并使用空对象调用 setState()
,则无法再实现这些方法。
<另一个是它只是糟糕的编程风格。你在两个陈述中做了你可以做的一件事。
此外,这里没有实际的好处。在这两种情况下, render()
直到 setState()
(或<$ c $)之后才会被触发c> forceUpdate())被调用。
您声称需要这样做,而无需实际解释需要的内容。也许你想更详细地解决你的问题。可能有更好的解决方案。
最好使用框架而不是反对它。
UPDATE
来自以下评论:
我需要使用更改hasSubmit在下面。
好的,我现在明白了。如果您需要立即使用未来的州财产,最好的办法就是将其存储在本地变量中。
const hasSubmit = false;
this.setState({
hasSubmit:hasSubmit
});
if(hasSubmit){
//将使用`hasSubmit`的代码...
I want to change the value for the hasSubmit
key, like in the First Code section. I know this is not recommended. But the second code is asynchronous and I don't want to use the callback function of setState
.
- What is the difference of
this.state
andsetState
? - Is there any way to change state value
hasSubmit
immediately?
First Code:
this.state.hasSubmit = false this.setState({}) //Code that will use `hasSubmit`.
Second code:
this.setState({ hasSubmit: false, }); //Code that will use `hasSubmit`.
ADD:
The scenario is that:
hasSubmit
setfalse
ingetInitialState()
.hasSubmit
will change tofalse
when I clicksubmit
button.hasSubmit
will change totrue
when submitted.
First click submit
has no problem and hasSubmit
will be set to true
.
But second click submit
will be wrong using the Second asynchronous code
, because the hasSubmit
is still true
, while the First Code
can resolve the problem.
Here's what the React docs say:
NEVER mutate
this.state
directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable.
setState()
does not immediately mutate this.state but creates a pending state transition. Accessingthis.state
after calling this method can potentially return the existing value.There is no guarantee of synchronous operation of calls to setState and calls may be batched for performance gains.
setState()
will always trigger a re-render unless conditional rendering logic is implemented inshouldComponentUpdate()
.If mutable objects are being used and the logic cannot be implemented in
shouldComponentUpdate()
, callingsetState()
only when the new state differs from the previous state will avoid unnecessary re-renders.
It's always sensible to use APIs in the way they were designed. If the docs say don't mutate your state, then you'd better not mutate your state.
Whilst setState()
might be technically asynchronous, it's certainly not slow in any noticeable way. The component's render()
function will be called in pretty short order.
One drawback of setting state directly is that React's lifecycle methods - shouldComponentUpdate()
, componentWillUpdate()
, componentDidUpdate()
- depend on state transitions being called with setState()
. If you change the state directly and call setState()
with an empty object, you can no longer implement those methods.
Another is that it's just bad programming style. You're doing in two statements what you could be doing in one.
Moreover, there's no actual benefit here. In both cases, render()
is not going to be triggered until after setState()
(or forceUpdate()
) is called.
You claim a need to do this without actually explaining what that need is. Perhaps you'd like to detail your problem a little more. There's probably a better solution.
It's best to work with the framework rather than against it.
UPDATE
From the comments below:
The need is that I want use the changed hasSubmit in below.
OK, I understand now. If you need to immediately use the future state property, your best bet is just to store it in a local variable.
const hasSubmit = false;
this.setState({
hasSubmit: hasSubmit
});
if (hasSubmit) {
// Code that will use `hasSubmit` ...
这篇关于ReactJS中this.state和this.setstate的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!