草庐IT

setstate

全部标签

javascript - ReactJS:为什么在setState更改状态时调用构造函数

我是ReactJS的新手,我制作了一个应用程序,您可以在其中提交姓名和电子邮件。姓名和邮件应显示在页面底部的列表中。它会显示一小段时间,然后调用构造函数并清除状态和列表。为什么在状态改变后调用构造函数?我以为构造函数只运行一次,然后render方法在setState()更改状态后运行。classAppextendsReact.Component{constructor(props){super(props);console.log("Appconstructor");this.state={signedUpPeople:[]};this.signUp=this.signUp.bind(

javascript - React setState 在 while 循环期间不设置状态

我想避免数据倍增,所以我想创建一个循环来为不同的site_id调用我的数据提供者。我创建了一个while循环并在此while循环中设置状态值。我意识到从我的2元素数组(我有2个站点)中只有1个被设置在状态中,而另一个没有。classDashboardextendsComponent{state={username:localStorage.getItem('username'),siteid:[{id:1,daily:"EKdaily",weekly:"EKweekly",monthly:"EKmonthly",total:"EKtotal",},{id:2,daily:"AKdail

javascript - 在循环的每次迭代中都使用 setState 是不好的做法吗?

这是一个小代码片段:asynccomponentDidMount(){...this.state.postList.forEach(element=>{this.fetchItem(element);});}asyncfetchItem(query){...this.setState(previousState=>{constlist=[...previousState.data,data];return{data:list};});}我很想知道在forEach循环的每次迭代中使用setState是否是个坏主意。我怀疑它会影响性能,但我想确定地知道,因为这似乎是解决此问题的最简单方法。

javascript - ReactJS onClick setState 到不同的元素

我是新手,遇到了一些小问题。也许有人可以帮助我。所以问题是我无法使用onCLick函数触发我想要的元素。现在我试图在时删除导航importReactfrom"react";importReactDOMfrom"react-dom";exportdefaultclassNavextendsReact.Component{constructor(){super();this.state={navStatus:"navHide"};}navClose(){varnavOpened=document.getElementById("myNav");navOpened.setState({nav

javascript - Bootstrap 开关 setState 给出 js 错误

我正在尝试使用来自Ajax的条件返回值来更改Bootstrap开关的状态,HTML,if(objData.limit==='Yes'){$('#limit').bootstrapSwitch('setState',true);$("#limit_amount").prop("readonly",false);}else{$('#limit').bootstrapSwitch('setState',false);$("#limit_amount").prop("readonly",true);}但它抛出如下错误..UncaughtTypeError:Cannotreadproperty'

javascript - 组件上的异步等待已挂载

这是我的componentDidMount方法。我想设置当前用户的状态,然后在设置该用户时调用该函数。我该怎么做?componentDidMount=()=>{firebase.auth().onAuthStateChanged((user)=>{if(user){this.setState({user:user})}});this.props.retrieveMatches(this.state.user.uid)}我试过使用async/await但我在这里没有正确使用它:asynccomponentDidMount=()=>{awaitfirebase.auth().onAuthS

javascript - 无法使用绑定(bind)到 `this.setState` 的对象文字调用 `partialState`,因为字符串 [1] 与字符串文字 [2] 不兼容

我一直在用头撞墙试图让它工作,有什么建议吗?我在这里使用reactwithflow。我很难理解这些代码注释的东西,同时我也在学习。起初它是压倒性的,但在我花了一些时间在谷歌上搜索远程关闭的任何东西之后,我已经走到了死胡同。帮助?//@flowimportReact,{Component}from'react';importShowCardfrom'./ShowCard';importHeaderfrom'./Header';typeProps={shows:Array};typeState={searchTerm:""};classSearchextendsComponent{hand

javascript - 超过最大更新深度。当组件在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState 时,可能会发生这种情况

我正在制作这个Conway的生命游戏React项目,它工作得很好,但是当我添加最后几个按钮来清除棋盘时,React的一些其他功能给了我这个错误Maximumupdatedepthexceeded.ThiscanhappenwhenacomponentrepeatedlycallssetStateinsidecomponentWillUpdateorcomponentDidUpdate.Reactlimitsthenumberofnestedupdatestopreventinfiniteloops.从它向我展示的代码片段来看,clear()函数似乎是这里的问题,但我认为我没有在rend

javascript - 来自 React Material UI MenuItem 单击事件的 setState

我正在尝试使用MaterialUI根据下拉选择更改状态。问题是下面的代码(精简)返回列表项,所以我不知道下一步该做什么。从handleClose方法中的console.log(event.target)返回精简组件:事件从API调用中获取。感谢您的任何想法。importReact,{Component}from"react";importButtonfrom"@material-ui/core/Button";importMenufrom"@material-ui/core/Menu";importMenuItemfrom"@material-ui/core/MenuItem";cla

javascript - 在 componentWillUpdate 或 componentDidUpdate 中重复调用 setState?

我试图找出作为Prop传入的React组件中背景图像的方向。我首先创建一个图像对象并将其src设置为新图像:getImage(){constsrc=this.props.url;constimage=newImage();image.src=src;this.setState({height:image.height,width:image.width});}在使用高度和宽度更新状态后,我尝试在componentDidUpdate()中调用getOrientation():getOrientation(){const{height,width}=this.state;if(height