0

I have an array that shows a set of items attached to a user. The array checks it first if the current user is on the item's inventory and then displayed as 'user name' and 'date borrowed' on a table. The adding feature is done in a modal, and suppose to update the table.

The problem is everytime I add, delete or update, the table doesn't update at all. Also this table is an expandend component of another table (react-data-table-component)

Here is the useState, and useEffect of my table:

const InventoryTable= ({ 
selectedUser, 
items, 
getItems, 
getUsers 
}) => { 
   useEffect(() => { 
   getItems(); 
   getUsers(); 
  }, []); 

  const [data, setData] = useState([]);

  useEffect(() => {
    let data= [];
    data= items?.filter((item) =>
      item?.users.some(
        (user) => parseInt(user?.id) === parseInt(selectedUser?._id)
      )
    );

    setData(data);
  }, []);

Note: selectedUser, is the user from the main table that was selected and this current table is to show the itms attached to it.

If I add data on the setData(data); }, []); it crashes. Adding data, selectedUser, and items on the dependency arrays loads it non-stop that causes to crash the page

4
  • We need more info like whats the error and probably not related but why are you using the question mark at the end of your variables ex: items?, selectedUser?, item? Commented Sep 26, 2020 at 14:06
  • There was no error at all. It successfully adds, but doesn't refresh the table Commented Sep 26, 2020 at 14:08
  • 2
    Using '?' at the end of your variable is for optional chaining operator to assign conditionally without hitting an exception. Well I just learned something I didn't know. Commented Sep 26, 2020 at 14:10
  • useState({}) contain an object maybe set useState([]) with an array Commented Sep 26, 2020 at 14:16

4 Answers 4

2

useEffect method takes 2 parameters as input.

  1. callback
  2. dependency

If the dependency is empty array it will be called in the similar way as Class Component with componentDidMount and componentWillUnmount lifecycle methods.

if dependency is present then the callback will be called after the UI in painted.

So clearly you have missed required dependency

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

5 Comments

Hello, i tried to add the data, items, and selectedUser on the dependecy array, but it crashes. and i cant seem to find the source of error. If i try to insert the selectedUser only on the dependency array, it doesn't wok
pass only items, selectedUser if you want to update based on those tow values. Also set array as initial state of data useState({}) -> useState([]) as filter output will be an array.
I actually wanted to update the data because it is used to show the value on the table
ya. but you want to update data here. not listen to changes in data? Adding data as dependency might trigger infinite loop of updates.
yeah. it does trigger infinite loops. if i tried adding selectedUser and items doesn't work at all.
1

I'm not sure that i understand the whole concepts of your code because it is just a small part of the code. But useEffect() will run accordingly if you want it to run as ComponenentDidMount you will use the code that you said above however in your case you want to update delete add it means you want to detect the change in data so you need to include data within the brackets like this

`useEffect(() => {

 let data= [];
data= items?.filter((item) =>
  item?.users.some(
    (user) => parseInt(user?.id) === parseInt(selectedUser?._id)
  )
);
setData(data);

}, [data,items,selectedUser]);`

5 Comments

adding data within the brackets crashes the page. and its weird there's no error at all
I don't actually understand why you try to set your Data in useEffect(). I don't recommend you using UseEffect unless you are dealing with codes that you want them to execute while the component mount and unmount. and if you specifically want to detect a change of state of and take action on that change of state you will use UseEffect() method with the bracket of the data you want to detect its change.Other wise i dont recommend you however if you insist show me your component that contains the useEffect
This is before my component: const InventoryTable= ({ selectedUser, items, getItems, getUsers }) => { useEffect(() => { getItems(); getUsers(); }, []); const [data, setData] = useState({});
You should create a codepen might be easier for us.
Well, I have seen your code. and Indeed it will not add delete or update because the function is called only once when the component mounts so what you have to do is if you are familiar with useReducer you should use that instead of useEffect. Besides i still have no idea where adding and updating happens. we can't help you with this limited information.
0

Seems you forgot to pass items and selectedUser to effect dependency array.

1 Comment

It doesn't work. I don't know why it crashes if i add something inside the dependency array
0

Maybe I am wrong, but have you tried renaming data property inside useEffect? It should have problem, that you are setting state without set callback. Try also set useState default value to [] instead of {} (object).

1 Comment

if you are not sure about answer and are only suggesting, try commenting instead of posting answer.

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.