I have a context object with an object holding all the information I need for a specific section of my application. The object contains some arrays.
Data
{
groupname: 'a',
members: {
regular: [
{ id: 1, name: 'x' },
{ id: 2, name: 'y' },
],
premium: []
}
}
I have a form where I can create a new member. Once the member is created I have a function inside a hook where I call the addMember function to add this newly created member.
function useGroup({ initialGroup: GroupType }: Props) {
const [group, setGroup] = useState<GroupType>(initialGroup)
const addMember = (member) => {
group.members.regular = [...group.members.regular, member]
setGroup(group)
}
return {
group,
addMember
}
}
I can use a console log and see this working effectively, however this is not being reflected in my UI. I can see the items when I refresh the page. This is telling me that the UI isnt 'refreshing' when I push the extra item inside my state object.
I feel as if this is because it is a nested array inside of an object. Should I split the members out into there own 'useState' arrays or is there something I am missing to have my UI reflect this being added?