2

I have a very basic question I have an array of JSONs and I could easily coopy them in functional component using

const [elements, setElements] = useState([]);
  useEffect(() => {
    const res = async () => {
      const result = await axios.get('/data');
      const data = result.data;
      setElements(elements => [...elements, ...data]);
    };
    res();
  }, []);

I want to convert them into class based components and when I try to copy in setState I get []. Code is below

import React, { Component } from 'react';
import './App.css';
import axios from 'axios';
import Element from './components/Element';
class App extends Component {
  constructor(props) {
    super(props);
    this.state = { elements: [] };
  }
  componentDidMount() {
    const res = async () => {
      const result = await axios.get('/data');
      const data = result.data;
      console.log(data);
      this.setState({ elements: data });
    };
    res();
  }
  render() {
    console.log(this.state.elements);
    return (
      <div className='wrapper'>
        <div id='table'>
          {this.state.elements.map(element => (
            <Element elements={element} key={element._id} />
          ))}
        </div>
      </div>
    );
  }
}

export default App;

Here is a JSON which I am getting

(119) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]   
2
  • this.setState({ elements: data }) will solve your issue Commented Oct 7, 2019 at 18:20
  • Did not work updated full code Commented Oct 7, 2019 at 18:33

2 Answers 2

2

The issue is with how you're trying to set state on your React component, maybe you could try something like the following:

this.setState({ elements: data});

Can you try updating your componentDidMount() to the following:

async componentDidMount() {
 const {data} = await Axios.get('/data');
 this.setState({ elements: data });
}

Hopefully that helps!

Sign up to request clarification or add additional context in comments.

2 Comments

setState accepts a function or an object, both patterns are valid
Try updating your componentDidMount() method, I updated my answer for how it should look
1

to make code equal it should be written as this

this.setState(state => ({
   elements: [...state.elements, ...data]
}));

1 Comment

@vinayak_shahdeo i also updated ny answer, hopefully it will work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.