#react issues once again

1 messages · Page 1 of 1 (latest)

dark steppe
#

yes more react issues

    const initialProducts = props.products;
    const [products, setProducts] = useState(initialProducts)

    toast.info(initialProducts.length) // 250
    toast.info(`We Have ${initialProducts.length} Initially`) // We Have 250 Initially
    toast.info(`${products.length} Are Set`) // 0 Are Set

output is attached

dosent make sense since i litterally pass initialProducts as the initial value into my useState but for some reason it dosent set?

slate field
#

Since it comes from props it might be that the parent component passes a default value of 0 until the amount of components is loaded?

dark steppe
# slate field Are you sure you never pass 0?

it would only pass 0 before they have been loaded, when they are loaded in a diffrent file it updates another useState that updates my loadedProducts (then passes to the code provided)
the screenshot is the output when the products have been 'loaded' and the loadedProducts useState in my other file has been updated with the products

#

i will do some more tests later, only just woke up

slate field
#

I suspect that you're being handed an empty Array via props

dark steppe
#

i'm not

#

look at the code

#

and the output

#

shows the length is 250

#

when i pass the products prop

slate field
#

useState is async so logging is done before products (the getter) has a value.

dark steppe
#

oh

dark steppe
#

so how do i fix it?

#

cause i am not particully sure what to do

#

just await useState ?

#

should i use useEffect?

#

ive been reading docs and it seems like i should use this but im not 100% sure

noble violet
#

Did you figure it out? @dark steppe

dark steppe
#

unfortunatly not, to be honest i didnt really check this part again because ive been working on adding other modules to my program, but it would be great to get it fixed

noble violet
#

Can you make a sandbox?

dark steppe
#

yh will do it when im home

dark steppe
#

@noble violet how do i save codesandbox?

#

on the thing im working on

#

i did this like 5 mins ago and lost everything i wanna save just to be sure

#

ok right @noble violet ive mad a simplified version that gives the same results

#

give me 2 sec

#

@noble violet

if you press test get prods button it will load the products from an api

but even though in the useState at the top of ProductsListTable , i have those products (25 products) i have to change the input for them to be set properly

noble violet
#

@dark steppe why are you storing the props in state?

dark steppe
#

for what func?

#

app or the productslisttable

noble violet
#

the latter

dark steppe
#

so that it re-renders when the title search is updated

#

and in App there is a usestate just incase the url changes so it gets the new products then renders them (there is normally an input to change the url)

noble violet
#

why don't you fetch data in the component itself?

#

instead of App

dark steppe
#

in my actual code there is a diffrent component that the inputs are in

#

and its because i just wanted the component to be a list for the products

noble violet
#

I see.... I am a bit busy today but I will take a look when I get time

dark steppe
#

products should be the exact same as initialProducts as i set it in the useState

#

(the comments are the outputs)

#

so products obviously isnt getting set correctly

#

i just cant figure out why

noble violet
#

I think you were right @dark steppe . You should use useEffect

#

And add props.products as dependency

#

Or whatever you want to respond to

dark steppe
#

wdym add it as a dependency?

noble violet
#

Put it in dependency array

dark steppe
#

oh i see

#

so i would have the useEffect in the ProductsListTable right?

#

and then

noble violet
#

Yes

dark steppe
#

when the state is updated the useEffect runs, which renders the table

noble violet
#

You have to set the state and that will cause a rerender

dark steppe
#

o alr

#

so setting the initial value wont rerender?

noble violet
dark steppe
#

thanks