logo
down
shadow

Array state variable is not being set during componentWillMount


Array state variable is not being set during componentWillMount

Content Index :

Array state variable is not being set during componentWillMount
Tag : javascript , By : boney M
Date : November 24 2020, 03:01 PM

this will help setState is asynchronous which means the state isn't updated yet by the time you call this.createGrid();
You can call createGrid after the state has been updated by passing in the function as the callback argument
this.setState({matrix: matrix}, this.createGrid.bind(this));

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

State is not updating in componentWillMount


Tag : reactjs , By : John Bentley
Date : November 01 2020, 12:01 AM
wish help you to fix your issue If you want to perform any asynchronous requests, I suggest you perform them in the componentDidMount lifecycle method. This is the suggested path based on the Reactjs documentation for componentDidMount.

Can't set state in componentWillMount


Tag : javascript , By : hsdfhksh
Date : November 14 2020, 11:01 PM
wish helps you In your case, your setState() won't work because you're using setState() inside an async callback
Working Fiddle: https://jsfiddle.net/xytma20g/3/
export default class Chat extends Component {
  constructor(props){
    super(props);

    this.state = {
      messages : [],
      message : '',
    };
    this.socket = io('/api/');
    this.onSubmitMessage = this.onSubmitMessage.bind(this);
    this.onInputChange = this.onInputChange.bind(this);
  }

  componentWillMount() {
    axios.get(`api/messages`)
      .then((result) => {
        const messages = result.data
        console.log("COMPONENT WILL Mount messages : ", messages);
        this.setState({ 
          messages: [ ...messages.content ] 
        })
  })

  render(){
    if(this.state.messages.length === 0){
     return false //return false or a <Loader/> when you don't have anything in your message[]
    }

   //rest of your render.
  }
}; 

componentWillMount fails to set the state


Tag : reactjs , By : janik
Date : March 29 2020, 07:55 AM

react: cannot set state in componentWillMount


Tag : react-native , By : Mahyar Sepehr
Date : March 29 2020, 07:55 AM
With these it helps First Don't use componentWillMount() to setState. componentWillMount() is called before the component is intialized (rendered) therefore state cannot be set on an unintialized component, use componentDidMount() and set state, checkout the following link for more information regarding this,
https://vasanthk.gitbooks.io/react-bits/anti-patterns/04.setState-in-componentWillMount.html

State not mounting with componentWillMount


Tag : reactjs , By : user158193
Date : October 09 2020, 01:00 AM
To fix this issue A few things here.
Your state is not changing because the function in which you call setState has a different value for this than you're expecting. If you check the console, you should see an error indicating that this.setState is not a function. You need to change the following:
window.FB.api('/me', 'GET', {"fields":"posts{created_time,likes,comments,picture,full_picture,shares,message},name,picture","access_token":this.state.disPageTken},
function(response) {
  console.log(response)
window.FB.api('/me', 'GET', {"fields":"posts{created_time,likes,comments,picture,full_picture,shares,message},name,picture","access_token":this.state.disPageTken},
(response) => {
  console.log(response)
Related Posts Related QUESTIONS :
  • In IE11 background colors not working on printing
  • TypeScript interface signature "(): string"
  • Is synchronous XMLHttpRequest supported in Internet Explorer 10 and 11?
  • Axios function not returning any values with js
  • How to use firestore TIMESTAMP to create time-stamp in JavaScript
  • Instantiate subclass without constructing
  • Outputing a created object in javascript to a server side JSON file
  • Transition the numeric labels in a bar chart
  • createBottomTabNavigator: hide just one tab from the tabBar
  • iife vs simple statement behavior with document.title
  • showDialogPopup Refresh Page after Click
  • Child component does not set the initial value passed from the parent: ReactJS
  • Nodejs javascript added to button not responding
  • Trying to define a promise.all
  • Python GEE to extract featurecollection timeseries from Landsat imageries
  • How to write negative binary number?
  • Unable to set property of individual object
  • Why Named Function Expression itself cannot assign Name to another Value?
  • How to reset scroll position on single page pagination
  • How to scroll to bottom of page, when a new message is sent - Socket.io
  • How to reconstruct audio blob from a base64 encoded String?
  • How to calculate numbers within an array of objects
  • Using JavaScript to change CSS properties of SVG circles for all circles that was not clicked
  • Is there value in propTypeing repeat required properties at every level of nested components?
  • Call function if variable does not exist in a filter
  • localStorage value doesn't get updated automatically
  • React not updating state with setState correctly inside promises
  • Direct native JavaScript or jQuery method to get containing block of an element
  • Dynamic JSON object to Html Table
  • How to send Id and file to same controller?
  • How to Make a jQuery Table Cell(td) clickable to Run a Function?
  • Prevent the duplicate function onchange event jQuery
  • Reactjs Input Upload doesn't return local URL
  • An unexpected 'StartObject' node was found when reading from the JSON reader. A 'PrimitiveValue' node was expected
  • How to filter a JSON Array, with an eventListener input value at keyup
  • How to separate the different properties of filter in css?
  • Why dont work change event of bootstrap input spinner?
  • How to hide overflow text content in react
  • Is there another way to write line 6
  • Cypress - how to find by text content?
  • How to setstate for the dynamic checkboxes in a loop in react?
  • Is display:none safe?
  • How to create a form to change part of a href url of a button?
  • Put many function inside function in Javascript
  • how to exit from loop after a ajax call
  • Do Angular Reactive Form Validator.min/max check Perform Type Conversions before checking the value?
  • How to Replace Text Within Script Tags Using JQuery
  • add variable to object if not undefined using short-hand
  • How can I effectively curve my items inside a list?
  • Add new record Keyboard Shortcut on Kendo Grid Mvc
  • Time and Date running on page
  • Looping through page links puppeteer doesn't return values from the newly loaded page
  • Problem with Navigator.geoLocation.getCurrentPosition not working
  • Trying to change the id of an element with Javascript
  • Embedded Schema Inserting Blank in Meteor using Collection2
  • avoid useEffect to render at first load?
  • Why is this CSS transition only working correctly on the some clicks? How to solve it?
  • Interpolated values does not reflect the latest values in an Angular app
  • Javascript classes define attribute in constructor or outside the class
  • How to block scroll event passing through to iframe until iframe is active?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com