R
roblox-ts3mo ago
Ego*

First React Code Review

so this is my first time using react roblox and I'm not sure if i did any bad practices in this code, it's working without lag but obs lags my laptop, also sorry PepeElToro I'm only using this background right now as a placeholder until i make/find a grid, also this is not a storybook plugin
Solution:
useEffect needs an array of dependencies
Jump to solution
19 Replies
Ego*
Ego*OP3mo ago
Ego*
Ego*OP3mo ago
ignore the const [canvasPos, setCanvasPos] = useBinding<UDim2>(UDim2.fromScale(0.5, 0.5)); line, it was for testing something and i forgot to remove it also is there a way to be able to test it inside ui labs without having to switch to place view ? -# tiny bump
Solution
PepeElToro41
PepeElToro413mo ago
useEffect needs an array of dependencies
PepeElToro41
PepeElToro413mo ago
so it can only run when something changes if you dont have dependencies, you need to provide an empty array because if you dont provide anything it will run it on every rerender setOffset would be better to be a binding
Ego*
Ego*OP3mo ago
oh I forgot about that what's the difference between Binding and State ? I tried reading the docs but I don't think i understand
duck
duck3mo ago
when binding changes, it doesn't trigger rerender, but when states change, it triggers a rerender. A rerender means the function is called again and rerunning all the hooks that are "runnable", for example a useEffect without dependency array or something else.
Ego*
Ego*OP3mo ago
ah alright but wouldn't it not update the UI if it didn't re-render ?
PepeElToro41
PepeElToro413mo ago
it binds to a property react detects when you assign a binding to a property and listens to it
Ego*
Ego*OP3mo ago
I think I'm missing some fundamentals of working with react, isn't re-rendering the way to change properties, so does binding a binding to a property just update that property alone without triggering a re-render ?
PepeElToro41
PepeElToro413mo ago
not quite, rerenders its like deleting and creating your ui again it doesnt actually do that but yeah its the advantage of react if you have used something like iris, its similar
Ego*
Ego*OP3mo ago
that sounds like really useful knowledge I have only worked with react before
PepeElToro41
PepeElToro413mo ago
yeah, rerendering calls your function again, meaning that you will create your elements again so its for changing anything running effects, creating children, properties
Ego*
Ego*OP3mo ago
so does that mean if the player let's say was in the inventory and the player had scrolled a bit and then a state changed which triggers a re-render, would that mean destroying and recreating the ui which would reset the canvas position property which the player had changed when he scrolled ?
PepeElToro41
PepeElToro413mo ago
no, its similar to iris, iris runs your render function every frame and it compares to see what to destroy or change
Ego*
Ego*OP3mo ago
alright thanks a lot man
PepeElToro41
PepeElToro413mo ago
yeah thats why in your state, you get a normal value
Ego*
Ego*OP3mo ago
pepe and duck I appreciate the help a lot, which msg should I set as the solution to this post ?
PepeElToro41
PepeElToro413mo ago
not a source or a state value
Ego*
Ego*OP3mo ago
I guess ima just mark the message of the useEffect dependencies and other members can read from there if they stumbled across this post

Did you find this page helpful?