草庐IT

SetState

全部标签

javascript - 为什么在 React 的#setState 中使用函数可以解决异步问题?

来自docs,它说“React可以将多个setState()调用批处理到单个更新中以提高性能”,因此它建议使用函数而不是对象作为setState的参数。这如何解决问题?//Wrongthis.setState({counter:this.state.counter+this.props.increment,});//Correctthis.setState((prevState,props)=>({counter:prevState.counter+props.increment})); 最佳答案 当您将一个对象传递给setStat

javascript - 在循环中调用 setState 只更新状态 1 次

在循环中调用setSate()是否会阻止它多次更新状态?我有averybasicjsbin这突出了我所看到的问题。有两个按钮。一个将状态计数器更新1。另一个在循环中调用One的底层函数——这似乎会多次更新状态。我知道这个问题的几种解决方案,但我想确保我首先理解了这里的底层机制。为什么不能在循环中调用setState?我是否对其进行了笨拙的编码,从而阻碍了预期的效果? 最佳答案 来自ReactDocs:setState()enqueueschangestothecomponentstateandtellsReactthatthisco

javascript - 可以将 setState 函数的 prevState 参数视​​为可变的吗?

我知道this.state不应该直接修改,而应该使用setState。由此我推断prevState也应该被视为不可变的,而setState应该总是看起来像这样:this.setState((prevState)=>{//Createanewobjectinstancetoreturnconststate={...prevState};state.counter=state.counter+1;returnstate;});或者更深的嵌套:this.setState((prevState)=>{//Createanewobjectinstancetoreturnconststate={.

javascript - setInterval 与 React 中的 setState

我有一个计时器使用setInterval()在React组件中,我不确定关于使用state开始和停止此间隔的最佳实践是什么.我遇到了一些异步问题。假设我的React组件中有一组链接可以很好地呈现和执行回调:letlinks=[10,50,100,500,1000].map((num)=>{return(this.switchNums(num)}to={`/somePath/${num}`}>{num})})这是switchNums()功能,我希望它重置现有的计时器:switchNums(num){this.stopTimer()this.reset(num)}这是startTimer(

javascript - 警告 : Can't call setState (or forceUpdate) on an unmounted component

我每次登录都会收到这个警告,Warning:Can'tcallsetState(orforceUpdate)onanunmountedcomponent.Thisisano-op,butitindicatesamemoryleakinyourapplication.Tofix,cancelallsubscriptionsandasynchronoustasksinthecomponentWillUnmountmethod.这是我的代码:授权页面.jshandleLoginSubmit=(e)=>{e.preventDefault()let{email,password}=this.st

javascript - 带有键但没有值的 react.js setState 调用?

刚开始学习react.js和javascript。我正在浏览facebook的github上的所有文档,但遇到了这个问题。在Calculator类的handleCelsiusChange方法中Liftingstateup章节有这一行:this.setState({scale:'c',value});因此scale将获得值“c”。好的。但是,这个值(value)只是在那里吗?不应该是键值对吗?我检查了explanationofsetState():Thefirstargumentcanbeanobject(containingzeroormorekeystoupdate)orafunct

javascript - ReactJS - 数组中对象键的setState

所以我已经研究了一段时间,觉得最好重构我的代码,以便将状态设置为对象数组。我想要做的是在单击按钮时增加一个数字。我在一个组件中有一个回调函数,它触发一个函数来更新状态……但是我很难定位对象中的键值。我的初始状态是这样的:getInitialState:function(){return{items:[{links:'zest',trackId:1023,songTitle:'zknowtheothers',artist:'zuvet',upVotes:0},{links:'alpha',trackId:987,songTitle:'ass',artist:'arme',upVotes:

javascript - Reactjs-setState previous state为第一个参数,props为第二个参数

我在这篇官方文章中读到了以下几行:this.propsandthis.statemaybeupdatedasynchronously,youshouldnotrelyontheirvaluesforcalculatingthenextstate.任何人都可以通过示例向我解释以下代码试图实现什么。this.setState((prevState,props)=>({couter:prevState.counter+props.increment}));IamreferringtothisofficialwebsiteofreactjsReact.js 最佳答案

javascript - React setState 在第一次尝试时不起作用,但在第二次尝试时起作用?

我在单击按钮时调用了handleSelection方法,但是,如果我单击该按钮,当它到达this.setState({selectedFoods:新选择});。该方法中的所有其他内容都正确执行(正如我的各种console.logs告诉我的:))。第二次单击该按钮时,该方法中的所有内容都会再次执行,并且setState起作用。varFlavor=React.createClass({getInitialState:function(){return{foods:{},selectedFoods:[],affinities:[]};},componentDidMount:function(

javascript - 警告 : setState(. ..):无法在使用 redux/immutable 的现有状态转换期间更新

我遇到了错误Warning:setState(...):Cannotupdateduringanexistingstatetransition(suchaswithinrenderoranothercomponent'sconstructor).Rendermethodsshouldbeapurefunctionofpropsandstate;constructorside-effectsareananti-pattern,butcanbemovedtocomponentWillMount.我发现原因是constmapStateToProps=(state)=>{return{noti