Does bound function being reused across multiple instance of React Component ?
Hi, I met some problem with event handler binding in React ES6 class syntax
// Render() of parent component
render() {
const { trades, contracts } = this.props;
const allID = Object.keys(trades);
return (
<div>
{allID.map(id => {
return (
<TradePanel
key={id}
id={id}
{...this.props}
/>
);
}
})}
</div>
);
// child Component class
class TradePanel extends Component {
method1(e) {
const { id } = this.props;
....
}
render() {
return <input onChange={::this.method1} />
}
}
Problem happens when I create 2nd instance of child component, method1 is incorrectly bounded to this of 1st instance.
Other props are pass properly, the problems are with event handlers.
When render() is called, will the binding be re-evaluate? If parent component rerender, does it destroy the old children and reconstruct a new one ?
Regards ~
Additional findings ~
This problem only happens on
<input type="radio">
text input works properly
There is no autobinding for ES6 classes so any binding issues are likely happening at the transform level (the :: binding syntax is not a standard at this point). If you can reproduce in a test case on jsfiddle using more supported syntaxes, it will be easier to see if maybe there's a bug in React.
OK, I'll do it, but it's gonna take sometime, as I am occupied now, thanks =)
Unreproducible
@qingweibinary I'm having a similar issue, were you ever able to figure it out?
@yaycmyk
Would you try perform the binding in constructor instead of inside render() ?