1

I want to access state data.

How do I copy an array object I have into a data array?

enter image description here

-code

function TableList() {

  const [state, setState] = React.useState({
    columns: [
      { title: '로트번호', field: 'lote_id' },
      { title: '중량(kg)', field: 'item_weight' },
      { title: '수량', field: 'item_quantity' },
      { title: '제품코드', field: 'item_code' },
      { title: '제품명', field: 'item_name' },
      { title: '규격', field: 'standard' },
      { title: '재질', field: 'material' },
      { title: '공정명', field: 'process_name' },
      { title: '단중', field: 'unitweight' },
      { title: '납기일시', field: 'duedate' },
      { title: '단가', field: 'item_cost' },
      { title: '금액', field: 'item_price' },
    ],
    data: [],
  });


  useEffect(() =>{
    console.log("실행")
    console.log(state.data);

    axios.get('http://localhost:8080/api/item/1')
    .then( response => {
         //I want to copy the data of my output.list to state.data.
    })
    .catch( response => {
      console.log(response);
    })
  });

I want to copy my axios response data into the data array declared as useState. Please tell me the most effective way.

  1. How do I access the second argument, state.data?

  2. I want to access and put my output.list response data.

3
  • can you please add your code rather than an image of it? Commented Dec 27, 2019 at 0:28
  • Not quite sure what you are after. Could you provide an example of the desired output? Commented Dec 27, 2019 at 0:35
  • You mean you want to copy columns or some other array into data? Commented Dec 27, 2019 at 0:40

3 Answers 3

1

Not totally clear on what you're looking for, but if you just want to update the data prop on your state object, you can do it like this:

const arrayOfObjects = [{ id: 1 }, { id: 2 }];
const newState = Object.assign({}, state);
newState.data = arrayOfObjects;
setState(newState);
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you By the way axios.get ('http: // localhost: 8080 / api / item / 1') .then (response => { //console.log(response); var output = response && response.data; const newState = Object.assign ({}, state); newState.data = output.list; setState (newState); }) .catch (response => { console.log (response); }) When you write it like this, the output occurs several times.
You'd need to post the full code for the component to see where/how you are calling it.
useEffect(() =>{ console.log("실행") console.log(state.data); axios.get('localhost:8080/api/item/1') .then( response => { //console.log(response); var output = response && response.data; const newState = Object.assign({}, state); newState.data = output.list; setState(newState); }) .catch( response => { console.log(response); }) });
What function does the mounted () function of Vue.js in React.js?
Please add it to your answer, it's very tough to read like that.
|
0

This is not a great way to use useState.

const [columns,setColumns] = React.useState([bla,bla]);
const [data, setData] = React.useState([bla, bla]);

You should declare your state variables separately like above.

Comments

0
  useEffect(() =>{
    console.log("실행")
    console.log(state.data);

    axios.get('http://localhost:8080/api/item/1')
    .then( response => {
      //console.log(response);
      var output = response && response.data;

      const newState = Object.assign({}, state);
      newState.data = output.list;
      setState(newState);
    })
    .catch( response => {
      console.log(response);
    })
  });

I want to call it just once like the mounted function

Comments

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.