天道酬勤,学无止境

setstate

ReactJS 并发 SetState 竞争条件(ReactJS concurrent SetState race condition)

问题 我有一个像这样的组件结构 <A> <B> <C/> <C/> </B> <D> <E/> <E/> </D> </A> 想法是块 E 中的组件上的操作由组件 A 的函数处理到 A 的状态,然后作为道具传递给 B 和 C。 我知道,更好的方法是使用 Flux、pubsub-js 或其他 Store-message 系统,但希望有人能解释为什么我理解的最正确的解决方案不起作用。 从组件 E 的多个实例同时调用组件 A 的这个函数会导致竞争条件,状态只有一个变化(而不是每个函数调用都提供一个变化) updateState(value,index){ this.setState(update(this.state, { a: { [index]: { b: { $set: value } } } }) ); } 这里的功能更新来自 import update from 'react/lib/update'; 与 ReactJS 推荐做法背道而驰的糟糕解决方案,但效果很好: updateState(value,index){ this.state.a[index].b=value; this.forceUpdate(); ); } 我的问题是: 这是一个错误,多个同时 setState 调用了竞争条件,还是我做错了什么而没有理解它? 回答1 您可能希望将一个函数传递给setState

2021-09-21 01:52:55    分类:技术分享    reactjs   race-condition   setstate

Yii::app()->user->setState 在 yii 中?(Yii::app()->user->setState in yii?)

问题 我想在 yii 中了解这一点: Yii::app()->user->setState('key','value'); 如果我有一个大小为 10 的数组并且我将此数组设置为如下状态: Yii::app()->user->setState('data',$dataArray) 我有另一个大小为 15000 的数组,我将此数组设置为如下状态: Yii::app()->user->setState('data',$dataArray) 这个可以吗? 或者它会影响我的 Yii 项目的速度,即较大的 setState 数据大小,yii 项目的速度会变慢? 在Yii::app()->user->setState('password',$password)设置密码等机密数据是否安全? 回答1 由于安全问题变量,不要将密码存储在 setState 中,但如果您需要,请使用一些加密。 您可以在这里看到 setState 函数正在使用会话 http://www.yiiframework.com/doc/api/1.1/CWebUser#setState-detail 您可以在会话中存储任意数量的数据。 所有会话都存储在服务器上。会话的大小没有限制,但 PHP 可以占用的内存有限制:http://ca.php.net/manual/en/ini.core.php #ini.memory-limit

2021-09-14 00:35:17    分类:技术分享    php   session   yii   frameworks   setstate

I am attempting to use setState in React

I'm attempting to use setState function to a method in my App class.. example of the current code: addRandomContact() { this.setState({ actors: contacts.slice(0, 6) }) } I am expecting my contacts array to change from a length of 5 to 6. The error I am receiving is the following: TypeError: Cannot read property 'setState' of undefined addRandomContact

2021-09-09 05:00:23    分类:问答    reactjs   setstate

ReactJS with Sockets Nested JSON parsing data issue

I have a JSON file that has a structure like this "data.json" { "Object1": { "name": "1", "rank": "2" }, "Object2": { "name": "3", "rank": "4" } } and in my React code, I have my data to set my react state. I want to pass Object2 to a different component, but I can't seem to differentiate the data. // import packages import React, { Component } from "react"; import io from 'socket.io-client'; class App extends Component { constructor() { super(); this.state = { data: null }; this.socket = io('http://localhost:5000'); // Binders this.updateState = this.updateState.bind(this); }

2021-09-07 21:46:33    分类:问答    json   reactjs   setstate

Calling `this.setState()` breaks flow type checking on a prop in componentWillReceiveProps

I am getting a flow error on a prop that I know is a string when I call this.setState() right before it. If I move the setState() call after the line that uses the prop, the error goes away. The error I'm getting is: null This type is incompatible with the expected param type of string undefined This type is incompatible with the expected param type of string Both errors occur on the same line of code. Here is a stripped down version of the component. See the implementation of componentWillReceiveProps: import React, { Component } from "react"; import apiRequest from "../../services/apiRequest

2021-09-05 19:42:26    分类:问答    javascript   reactjs   flowtype   setstate

How to use map function for hooks useState properties dynamically

My question is almost the same as this one. In this case, the person has a map of states created in a hard coded way: const App = props => { const [state, changeState] = useState({ name: "", eventTitle: "", details: "", list: "", toggleIndex: "", editName: "", editEventTitle: "", editDetails: "", }); The difference is that I want to create those states dynamically, receiving them from another component. I tried something like this but it obviously did not work: const App = props => { const [state, changeState] = useState({ props.inputs.map((input, i) => input = "" ) }); Do you know any

2021-09-05 01:25:19    分类:问答    reactjs   react-hooks   setstate

Yii::app()->user->setState in yii?

I want to know about this in yii: Yii::app()->user->setState('key','value'); If I have a array of size 10 and I set this array in state like: Yii::app()->user->setState('data',$dataArray) I have another array of size 15000 and I set this array in state like: Yii::app()->user->setState('data',$dataArray) Is this Ok? Or It will effect the speed of my Yii project i.e. larger the setState data size ,slower will be the speed of yii project? Is it is secure way for confidential data like Password to set in Yii::app()->user->setState('password',$password)?

2021-09-03 01:58:45    分类:问答    php   session   yii   frameworks   setstate

Flutter Set State onPressed on RaisedButton

I am building a quiz app which reveals the explanation for the correct answer after the user submits their chosen answer. There are two buttons on the layout -- "Next Question" & "Submit Answer." In the initial state, the "Next Question" button is subtle as it is not clickable and only the "Submit Answer" buttons is clickable. Click Here to View the Layout of the Initial State When the "Submit Answer" button is clicked, two actions should happen: 1. The "Submit Answer" button then becomes subtle and not clickable and the "Next Question" button becomes bold and vibrant and, of course, clickable

2021-08-31 08:56:54    分类:问答    dart   flutter   setstate   flutter-layout

React setState slow

so I was programming an app with React and what happened is that I have a component with a fairly large list in its state. I am fetching a JSON file from network and then storing a filtered copy directly to component's state. Might be unoptimal solution BUT I think that's still okay and React should handle it I mean, it's just 10 kB. Anyway I decided to add a search input to my component and store its value to its state. Now I have both the large list and searchInput in its state which I am setStating every onChange and filtering the list based on that. And that is super slow. Every setState

2021-08-11 10:16:25    分类:问答    javascript   performance   reactjs   setstate

React filter method without mutating state

My current state has this: state = { items : [{id: 1, text: 'test words'}, {id: 2, text: 'another test'}] Here's my function to remove objects from the array, trying to avoid mutating state. handleRemove(passedInId) { const myItems = this.state.items const newArray = myItems.filter(item => item.id !== passedInId) this.setState(prevState => ({ items: prevState.items = newArray })) console.log('handle remove runned', passedInId, myItems, newArray) } It works perfectly but would like to know if it's not anti-pattern before moving on with my life Many THANKS!!

2021-08-01 03:37:36    分类:问答    javascript   reactjs   ecmascript-6   setstate