Hey guys,
I just want to know if there is a recommend way to pass props within a React porperty? For example, I’m having this property within my component:
The problem is that binding here is an anti pattern. My next solution would be to use an arrow function, but this is an anti pattern too. So what would be the way to use only one saveValue function without writing a new one for each component?
Using arrow functions or binding in JSX is a bad practice that hurts performance, because each render will create a new function, which means that the garbage collector will have more work than needed.
Which means that the following code is an anti pattern:
Because you will get a value comparison instead of a reference comparison. That way react will always see this property as a new property, forcing a rerender.
var funcCreator = () => () => {};
var funcCreator2 = () => funcCreator;
var isEq = (a, b) => a === b;
isEq(funcCreator(), funcCreator()); // false
isEq(funcCreator2(), funcCreator2()); // true
I wonder why you need the input id at all. Referring to elements sounds a bit of an unnecessary. Anyway, don’t expect to be able to copy/paste this example; and that it will work as is. But this is the general idea:
Yeah, this seems to work fine, but I guess I’ll run into another problem if I want to set a state value via this.setState, because I didn’t bind to my current component.
In my current case, this isn’t a problem, because I have a store and getting it’s values via properties. But would be interesting to know what would be the correct way to set state while also receiving a function parameter without doing a bind within a prop.
Because Facebook is explaining you how to get the most out of React? Some performance tweaks are just not really obvious. And perhaps you just don’t care about them for a simple todo app.
You don’t need to follow al the guidelines to start using it. Not feeling comfortable with React? Then don’t use it
Don’t blame the language for a framework or a company that just explains how to use their framework.
Javascript has it’s bads, but this is not one of them. Every language has by value vs by reference.