score:3
First initialize your state in your constructor like this
constructor(props) {
super(props);
this.state = {users : []} //initialize as array
}
Then instead of that.state = {users: json};
set your state using
that.setState({ users: json });
score:0
You already got the answer to use setState({ users: json })
, which is right.
As an alternative to initializing the array value like abul said, you could also do a conditional render, depending on the component state. I.e you can render a different component if the users aren't loaded yet.
render() {
const users = this.state.users;
return !users ?
<p>Loading..</p> :
<YourComponent users={users} />;
}
score:1
You should use the React setState()
method.
that.setState({ users: json });
Source: stackoverflow.com
Related Query
- ReactJS: What is the correct way to set a state value as array?
- What is the best way to add a value to an array in state
- What is the correct way of setting state variables in Reactjs and what is the difference between these approaches?
- what is the correct way to add data array to the array state as well
- What is the correct way to set state in React
- What is the correct way of updating state for which the value needed to be a combination of previous state and event
- What is the correct way to set State according to fetch data?
- ReactJS : What is the best way to give keys in array element
- In React, what is the right way to update nested array state items
- What is the best practice to set initial state in reactjs constructor redux?
- What is recommended way to determine if a Redux state empty array is the initial state or the result of an API call that returns an empty array?
- What is the correct way to handle a key event using useEffect() hook which on the other hand triggers local state changes?
- What is the Mobx way to do handle set state with callbacks
- What is the right way to toggle a value inside object state in React hooks?
- In Reactjs how do I update the correct value in the parent state with a callback that's been passed to a nested child component?
- React - How to use the current state value as a variable so i can set as a index inside an array
- Asp.NET Core + ReactJs - What is the correct way to store authentication information?
- Correct way to set default prop to the first item in prop array
- Correct way to handle value change of an input element mapped from array held in state
- What is the best way to set initial state to avoid error: "can not read property of undefined"?
- What is the correct way to set an element type passed as a parameter
- reactjs calculate sum of all the value present in state in a form of array of object
- What is the preferred way of mutating a big state array in ReactJS?
- What is the correct way to push jsx with embedded js variables on to an array
- What is the correct way to add to an array with splice in react
- How to prepend a value to an array inside the object of state in Reactjs
- What is the correct way to update state and render the component?
- What should be the correct inital value for redux state
- What is the best way to set default value of Props?
- What is the simplest way to add a value to state using material-ui form dialog?
More Query from same tag
- React: Wait until all assets are loaded
- React Hook useEffect has a missing dependency:
- ReactJS access to Virtual DOM reconciliation result
- Hiding components: adding a class or removing from the DOM?
- How to fill an 2D array with a 2D Smaller array in JavaScript
- react-router official example Jump to three pages failed
- Autocomplete disable styling? (Picture attached)
- Reactjs slick slide
- Type of generic abstract react component
- Webpack 5 - Uncaught ReferenceError: process is not defined
- How to implement vertical scrolling of a grid items within a grid in material ui?
- Justify-items: flex-end; Isn't working for me
- react js : display items using map method
- Manage react apollo with redux observable
- State is not being mapped to component props
- webpack minify with global variables and methods for selective files in React
- ReactJS Load Picture from Project, doesnt work somehow
- preloadedState when injecting asyncReducers
- Cloudinary return empty url on first try
- Styling components and react-bootstrap
- Having issues connecting to express backend with React
- Why not to use splice with spread operator to remove item from an array in react?
- How to avoid infinite re-rendering with event listener in React Component?
- ReactJS - Babel version problems
- Application Error after deploy in heroku MERN app
- Selecting created option on menu close/select blur using creatable component
- How to fix: Error: ENOENT: no such file or directory 'src/node_modules/native-base/Fonts'
- Verify request with axios-mock-adapter?
- uncaught typeerror INCLUDES is not a function
- Unterminated JSX contents - react