setstate

react setState sets to string instead of object

社会主义新天地 提交于 2019-12-12 04:48:53
问题 I am trying to fetch a big json file when the webpage has been loaded and then update react state with that data. Currently, I have this code get(url) { return new Promise((resolve, reject) => { const req = new XMLHttpRequest(); req.open('GET', url); req.onload = () => req.status === 200 ? resolve(req.response) : reject(Error(req.statusText)); req.onerror = (e) => reject(Error(`Network Error: ${e}`)); req.send(); }); } componentDidMount(){ this.get('https://x.com/data/tool.json').then(

Pass values to child component on click submit button - ReactJS, click twice

这一生的挚爱 提交于 2019-12-12 04:41:40
问题 I want to pass values from SearchInput (parent) to FetchData (child) component. It does not work properly, because I have to click twice to fetch data and this.props.loaded should be true after click on submit button. I know, that I should use callback function, but I dont know, which function and where. I'm started to learn ReactJS a week ago. import React, {Component} from "react"; import FetchData from "./FetchData"; export default class SearchInput extends Component { constructor(props) {

Flutter: Calling SetState() from another class

≡放荡痞女 提交于 2019-12-11 18:42:09
问题 I am trying to make a simple image that appears or disappears when a button is pushed. This button resides in a separate class to the image, so in Flutter this creates a massive headache of an issue. I have read many forums on this and I have tried all the solutions posed but none of them are working for me. What I am trying to do: class SinglePlayerMode extends StatefulWidget { @override SinglePlayerModeParentState createState() => SinglePlayerModeParentState(); } class

Constructing state object for all the columns of a data table for sorting : React JS

痞子三分冷 提交于 2019-12-11 18:19:05
问题 Need to construct a state object that will be helpful for maintaining fieldname and type of sort(ascending/descending). I am trying to implement sorting where the sorting on the server side happens by taking the column name and the type of sort(asc or des). I am maintaining a component for a data table where-in I attach a click handler that basically should give the field name that I click and the type of sorting. I can hardcode the fieldnam, But how will I map each column with type of

Haze about 'setState' in React official doc

て烟熏妆下的殇ゞ 提交于 2019-12-11 17:24:54
问题 I am reading react.js official docs. Here is one of them. I am confused about this paragraph: setState() will always lead to a re-render unless shouldComponentUpdate() returns false. If mutable objects are being used and conditional rendering logic cannot be implemented in shouldComponentUpdate(), calling setState() only when the new state differs from the previous state will avoid unnecessary re-renders. Question: Why calling setState() will avoid unnecessary re-renders if mutable objects

Dynamically add classes on based on state value and also change the children content based on parent's state change

心不动则不痛 提交于 2019-12-11 15:54:58
问题 I am implementing a setting page for an application. For each setting I have implemented a slider that has enabled(green) or disabled(red) state. But parent's settings is read only and is calculated based on the values of its children. Parent should also be changeable ; Parent on green should turn all children to green ; On red it should be red ; But pending it should be as is For this , I am using react-multi-toggle for this toggle switch. Also If I want to dynamically add background color

Forming the components based on the array of objects

不羁岁月 提交于 2019-12-11 15:54:31
问题 For a setting page for an application, I have implemented a slider that has enabled(green) or disabled(red) state. And parent's settings are calculated based on the values of its children. //Getting the switches configuration inside componnetDidMount something like this var obj = [ { parent_header_name: "parent1", children_info: [ { child_switch_name: "child1", isEnabled: true }, { child_switch_name: "child2", isEnabled: false } ] }, { parent_header_name: "parent2", children_info: [ { child

Reactjs setState asynchronous

落花浮王杯 提交于 2019-12-11 15:49:00
问题 I am building a little search engine and got following problem: Everytime I enter a query, the last letter is missing. I figured out, that it has something to do with setState and that it is not asynchronous ... But I can not come up with a solution fo my case. Her is my function: searchHandler = ({ target: { value } }) => { this.setState({ term: value }); this.updateMessage(value); if(this.state.message.length === 0){ this.setState({hasMoreItems: false}) this.componentDidMount(); }else{ var

Compute the parents state based on the data of the child components: ReactJS

落爺英雄遲暮 提交于 2019-12-11 14:27:59
问题 I am trying to implement a settings page where I have a global settings and some kind of child settings(in form of a slider). I have the following scenarios: 1)When all of the child settings is on , then parents switch state should be turned on state 2)When any of the child settings is off, then parents switch state should be switched to pending 3)When all of the child settings is off, then parents switch state should be switched to off state 4) Also On click of button, I need to get the

React change state in array (for loop)

一世执手 提交于 2019-12-11 11:32:39
问题 I have a State with flights, and I have a slider to change the max price to change visibility of the flight elements. maxpriceFilter() { var flightOffer = this.state.flightOffer; var sliderPrice = this.state.sliderPrice; for (var i = 0; i < flightOffer.length; i++) { if( flightOffer[i].price > sliderPrice) { this.setState( {[flightOffer[i].hiddenprice] : true} ); }; } This code is adding a undefined field with status true in the root of the state though.. I cant find any best practice on this