jjzjj

setstate

全部标签

javascript - ReactJS Array.push 函数在 setState 中不起作用

到目前为止,我正在制作一个包含3个问题的原始测验应用程序,全部为真或假。在我的handleContinue方法中,有一个调用将用户输入从radio表单推送到userAnswers数组。它在第一次运行handleContinue时运行良好,之后它抛出一个错误:UncaughtTypeError:this.state.userAnswers.pushisnotafunction(...)importReactfrom"react"exportdefaultclassQuestionsextendsReact.Component{constructor(props){super(props)

javascript - 在 React-Native 中访问 this.state

嗨,我是React的新手,请多多包涵。我想将地理位置存储为一个状态。看起来很漂亮,因为位置的任何变化都会触发渲染,这正是我想要的。在开发过程中,我有一个按钮,通过访问lastPosition手动触发事件。但是当我这样做的时候。状态是“未定义”。有什么线索吗?exportdefaultclassFetchProjectextendsComponent{constructor(props){super(props);this.state={initialPosition:'unknown',lastPosition:'unknown',};}//codethatsetslastpositi

javascript - 不使用 setState 修改 state 有什么风险?

我知道直接修改状态不用setState(...)不会自动更新UI,但我仍然可以这样做:this.state.myValue="foo";this.forceUpdate();我也知道React会等待特定时刻来一次更新多个组件,但是真的有什么令人信服的理由说明我不应该在没有setState(...)的情况下直接改变状态吗??有两种情况直接改变状态对我来说是有益的:如果我必须修改一个非常长的数组的元素,setState(...)的“集群更新”会带来性能提升与不是每次都浅复制整个数组所带来的性能提升相比,可以忽略不计。如果我在状态的2个不同属性中有2个对同一个对象的引用,并且我想修改这个对象

javascript - 功能性 setState 和 props

我很清楚为什么我们需要函数式setState以及它是如何工作的,例如this.setState((prevState,props)=>...);您可以像上面那样获取先前的状态作为参数。但是也要注意参数中的props。Here我在函数式setState中遇到了关于props的解释:Inaddition,italsoapplieswhentheupdatedependsonprops.Thesecanbecomestaleaswellwhenthereceivedpropsfromtheparentcomponenthavechangedbeforetheasynchronousexecu

javascript - 使用 map react setState 对象数组不起作用?

我在执行setState更改嵌套对象数组的值时遇到问题。下面的代码假设将id2的问题更改为answer:true但不是,怎么了?this.state={questions:[{id:1,answer:''},{id:2,answer:''},]}//Ihaveaclickeventsomewherethis.setState({questions:this.state.questions.map(q=>{if(q.id===2){return{...q,answer:true}}else{return{...q}}})},console.log(this.state.questions

javascript - 使用 setState() 更新嵌套的 React 状态属性的最佳替代方法是什么?

我一直在考虑在这些选项中使用ReactsetState()方法更新嵌套属性的最佳方法是什么。考虑到性能并避免与其他可能的并发状态更改发生冲突,我也对更有效的方法持开放态度。注:我正在使用一个扩展React.Component的类组件.如果您使用的是React.PureComponent更新嵌套属性时必须格外小心,因为如果不更改state的任何顶级属性,则可能不会触发重新渲染。.这是说明此问题的沙箱:CodeSandbox-ComponentvsPureComponentandnestedstatechanges回到这个问题-我在这里关心的是性能和其他并发之间可能存在的冲突setStat

javascript - 顺序 setState 调用未按预期工作

我正在创建一个带有动画的组件,该动画随css类切换而发生。示例的沙箱here.css类有条件地应用于transitioned字段,因此当transtioned字段从false变为真。问题:如果像这样修改状态,则不会发生动画:animateWithoutST=()=>{this.setState({transitioned:false},()=>this.setState({transitioned:true}))}但如果在setTimeout回调中调用第二个setState,它会起作用,如下所示:animateWithST=()=>{this.setState({transitione

javascript - 防止 this.state 与 setState 一起使用

Thereference状态:setState()doesnotalwaysimmediatelyupdatethecomponent.Itmaybatchordefertheupdateuntillater.Thismakesreadingthis.staterightaftercallingsetState()apotentialpitfall.Instead,usecomponentDidUpdateorasetStatecallback(setState(updater,callback)),eitherofwhichareguaranteedtofireaftertheupd

javascript - ReactJS SetState 不重新渲染

我有:工作屏幕handleSetView(mode,e){this.setState({view:mode});console.log(this.state.view)}render(){return(...)}右面板render(){return(Views...)}工作listrender(){varjobs=[];this.state.jobs.forEach((job)=>{jobs.push();});return({jobs});};问题是,View状态的改变不会重新渲染任何子元素。我怎样才能让它工作? 最佳答案 不确定

javascript - react : potential race condition for Controlled Components

theReacttutorial中有如下代码:classNameFormextendsReact.Component{constructor(props){super(props);this.state={value:''};this.handleChange=this.handleChange.bind(this);this.handleSubmit=this.handleSubmit.bind(this);}handleChange(event){this.setState({value:event.target.value});}handleSubmit(event){alert