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
19 Replies
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 bumpSolution
useEffect needs an array of dependencies
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 bindingoh I forgot about that
what's the difference between Binding and State ? I tried reading the docs but I don't think i understand
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.
ah alright
but wouldn't it not update the UI if it didn't re-render ?
it binds to a property
react detects when you assign a binding to a property
and listens to it
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 ?
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
that sounds like really useful knowledge
I have only worked with react before
yeah, rerendering calls your function again, meaning that you will create your elements again
so its for changing anything
running effects, creating children, properties
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 ?
no, its similar to iris, iris runs your render function every frame
and it compares to see what to destroy or change
alright thanks a lot man
yeah thats why in your state, you get a normal value
pepe and duck I appreciate the help a lot, which msg should I set as the solution to this post ?
not a source or a state value
I guess ima just mark the message of the useEffect dependencies and other members can read from there if they stumbled across this post