setstate

setState conditionally in react

喜欢而已 提交于 2019-12-05 20:13:45
I know some react but I am stuck in a weird situation. I have two inputs and a button, the button should be enabled when both inputs are not empty. So, I used a state property for each input value and also one property telling me if both inputs have value: this.state = { title: '', time :'', enabled : false } also I have a onChange for each input to set State accordingly: <input type="text" id="time" name="time" onChange={this.onChange.bind(this)} value={this.state.time}></input> <input type="text" id="title" name="title" onChange={this.onChange.bind(this)} value={this.state.title}></input>

How to use setState() in React to blank/clear the value of an array

試著忘記壹切 提交于 2019-12-05 10:51:38
I am trying to clear an array, but I'm having trouble. this.setState({warnErrorTypes:[]}) I'm not sure if I am dealing with a race condition, or what the specific issue is, but I can see that the value of my array is consistently wrong in the case that I need to reset its value to []. How does one replace an array that contains [1,2] with [] then subsequently [3] where the following are true: this.state.warnErrorTypes is an Array which starts out with [] Based on condition, 2 is pushed in Array Based on condition, 1 is pushed in Array. Based on condition, 3 is NOT pushed in Array Pause. User

Flutter: Does it matter what code is in setState()?

爷,独闯天下 提交于 2019-12-05 01:17:25
When we want a StatefulWidget to rebuild we call setState() but does it really matter if the code we type is inside that function or outside of it? Is this: class _ShoppingListState extends State<ShoppingList> { Set<Product> _shoppingCart = new Set<Product>(); void _handleCartChanged(Product product, bool inCart) { setState(() { if (inCart) _shoppingCart.add(product); else _shoppingCart.remove(product); }); } } the same as this: class _ShoppingListState extends State<ShoppingList> { Set<Product> _shoppingCart = new Set<Product>(); void _handleCartChanged(Product product, bool inCart) { if

How can I pass event to setState callback function?

早过忘川 提交于 2019-12-04 14:11:06
Is it possible in React to pass an external event to the callback function of setState? Example someFunc(event) { this.setState( { value: event.target.value }, () => { this.props.onChange(event); // <- cannot pass to here } ); } EDIT: See accepted solution below by Liam for an excellent answer, Here is the specific solution to my problem: Solution someFunc(event) { event.persist() // <- add this line and event should pass without a problem this.setState( { value: event.target.value }, () => { this.props.onChange(event); } ); } You need to extract the values or use e.persist() https://reactjs

How to add a new key value to react js state array?

泄露秘密 提交于 2019-12-04 08:46:00
I want to add new key value to array ınto state? I tell you what I want to do below. How can I do that? 1. Start state in consruture method `this.state = { files: [] }' After I doing set state with active files this.setState({files: activeFiles}) Screen my state { files: [ { key1: val1, key2: val2, key3: val3 }, { key1: val1, key2: val2, key3: val3 }, { key1: val1, key2: val2, key3: val3 } ] } How to add new key value for each file? The state I want { files: [ { key1: val1, key2: val2, key3: val3, key4: val4 }, { key1: val1, key2: val2, key3: val3, key4: val4 }, { key1: val1, key2: val2, key3:

ReactJS concurrent SetState race condition

我的未来我决定 提交于 2019-12-04 05:50:22
I have a component structure like this <A> <B> <C/> <C/> </B> <D> <E/> <E/> </D> </A> Idea is that actions on components in block E are processed by a function of component A to state of A and than passed down to B and C as props. I know, that better way was to use Flux, pubsub-js or other Store-message system, but hope if someone can explain why correct to the best of my understanding solution doesn't work. Calling this function of component A simalteneously from multiple instances of component E leads to race condition with only one change in state (instead of each function call providing a

Reactjs-setState previous state is the first argument, props as the second argument

故事扮演 提交于 2019-12-03 12:04:04
I have read in this official article these lines: this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state. Can anyone please explain to me what the following code is trying to achieve by giving an example. this.setState((prevState, props) => ({ couter: prevState.counter + props.increment })); I am referring to this official website of reactjs React.js They say you should do like that instead of the below example. // Wrong this.setState({ counter: this.state.counter + this.props.increment, }); They can't assure the state will

ReactJS with Sockets Nested JSON parsing data issue

青春壹個敷衍的年華 提交于 2019-12-02 09:08:21
问题 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(

React Native setState not a function

混江龙づ霸主 提交于 2019-12-02 03:10:42
问题 Can someone explain me why this.setState is not a function ? I do not see why my code has an error import React from 'react'; import axios from 'axios' import { StyleSheet, Text, View , Image} from 'react-native'; export default class App extends React.Component { constructor(){ super(); this.state = {res: []} } componentDidMount() { axios.get('https://api.github.com/repos/torvalds/linux/commits') .then(function (response) { this.setState({res: response}); }).catch(function (error) { console

ReactJS with Sockets Nested JSON parsing data issue

萝らか妹 提交于 2019-12-02 02:58:42
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); }