setstate

React Native setState not a function

◇◆丶佛笑我妖孽 提交于 2019-12-02 00:30:23
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.log(error); }); } } Thank you This is lexical scope issue. Use arrow function . .then((response) => {

Cannot update during an existing state transition

浪子不回头ぞ 提交于 2019-12-01 12:25:21
问题 My meteor project keeps crashing my browser when I load it up. I can only avoid browser crash if I comment out this.setState({input_36:currentApp.input_36}); in the App.jsx file. Can someone tell me how to fix my code so that the project can load without crashing and if you click on any hyper links in the <ul> , it will re-render the form? And make sure the links are WCAG compliant and search engine optimized by ensuring the href= attribute is there? Here's my project...in terminal command

Cannot update during an existing state transition

假如想象 提交于 2019-12-01 12:13:24
My meteor project keeps crashing my browser when I load it up. I can only avoid browser crash if I comment out this.setState({input_36:currentApp.input_36}); in the App.jsx file. Can someone tell me how to fix my code so that the project can load without crashing and if you click on any hyper links in the <ul> , it will re-render the form? And make sure the links are WCAG compliant and search engine optimized by ensuring the href= attribute is there? Here's my project...in terminal command line I do meteor create crudapp cd crudapp rm crudapp.js meteor remove autopublish meteor add react

Set state when testing functional component with useState() hook

試著忘記壹切 提交于 2019-12-01 04:12:20
When I tested class component with enzyme I could do wrapper.setState({}) to set state. How can I do the same now, when I am testing function component with useState() hook? For example in my component I have: const [mode, setMode] = useState("my value"); And I want to change mode inside my test When using state from hooks, your test must ignore implementation details like state in order to properly test it. You can still make sure the component passes the correct state into its children. You can find a great example in this blog post written by Kent C. Dodds. Here's a code expert from it with

React - Changing the state without using setState: Must avoid it?

ⅰ亾dé卋堺 提交于 2019-11-30 09:00:46
问题 My code works, but I have a best practice question: I have an array of objects in the state, and a user interaction will change a value of one object at a time. As far as I know, I'm not supposed to change the state directly, i should always use setState instead. If I want to avoid that with any price, I will deep clone the array by iteration, and change the clone. Then set the state to the clone. In my opinion avoiding to change the state that I will change later anyway is just decreasing my

setState() or markNeedsBuild called during build

人盡茶涼 提交于 2019-11-30 06:57:16
问题 class MyHome extends StatefulWidget { @override State<StatefulWidget> createState() => new MyHomePage2(); } class MyHomePage2 extends State<MyHome> { List items = new List(); buildlist(String s) { setState(() { print("entered buildlist" + s); List refresh = new List(); if (s == 'button0') { refresh = [ new Refreshments("Watermelon", 250), new Refreshments("Orange", 275), new Refreshments("Pine", 300), new Refreshments("Papaya", 225), new Refreshments("Apple", 250), ]; } else if (s == 'button1

Set state when testing functional component with useState() hook

那年仲夏 提交于 2019-11-30 03:09:29
问题 When I tested class component with enzyme I could do wrapper.setState({}) to set state. How can I do the same now, when I am testing function component with useState() hook? For example in my component I have: const [mode, setMode] = useState("my value"); And I want to change mode inside my test 回答1: When using state from hooks, your test must ignore implementation details like state in order to properly test it. You can still make sure the component passes the correct state into its children

When does React re-render after a setState call made inside an event handler

这一生的挚爱 提交于 2019-11-29 23:19:01
问题 From React DOCS: https://reactjs.org/docs/state-and-lifecycle.html State Updates May Be Asynchronous React may batch multiple setState() calls into a single update for performance. This makes total sense. If you have something like the function below, it would be very inefficient to re-render on every setState call const [state1,setState1] = useState(false); const [state2,setState2] = useState(false); const [state3,setState3] = useState(false); function handleClick() { setState1(true);

React - Changing the state without using setState: Must avoid it?

落花浮王杯 提交于 2019-11-29 10:08:34
My code works, but I have a best practice question: I have an array of objects in the state, and a user interaction will change a value of one object at a time. As far as I know, I'm not supposed to change the state directly, i should always use setState instead. If I want to avoid that with any price, I will deep clone the array by iteration, and change the clone. Then set the state to the clone. In my opinion avoiding to change the state that I will change later anyway is just decreasing my performance. Detailed version: this.state.data is an array of objects. It represents a list of topics

setState() or markNeedsBuild called during build

自作多情 提交于 2019-11-29 03:08:11
class MyHome extends StatefulWidget { @override State<StatefulWidget> createState() => new MyHomePage2(); } class MyHomePage2 extends State<MyHome> { List items = new List(); buildlist(String s) { setState(() { print("entered buildlist" + s); List refresh = new List(); if (s == 'button0') { refresh = [ new Refreshments("Watermelon", 250), new Refreshments("Orange", 275), new Refreshments("Pine", 300), new Refreshments("Papaya", 225), new Refreshments("Apple", 250), ]; } else if (s == 'button1') { refresh = [ new Refreshments("Pina Colada", 250), new Refreshments("Bloody Mary", 275), new