Last active
February 2, 2017 22:45
-
-
Save shuntksh/9840d3c5b42dc4b0f8e1282c3b056402 to your computer and use it in GitHub Desktop.
React.jsのrenderの戻り値の中で.bindで新しい関数を定義してはいけないわけ ref: http://qiita.com/shuntksh/items/fd81ca9aa31ea8f962e2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 悪い例:render関数内で新しい関数を定義 | |
class MyComponent extends React.Component { | |
render() { | |
const { value, handleChange } = this.props; | |
return <input value={value} onChange={handleChange.bind(this)} /> | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 悪い例:render関数内で新しい関数を定義 | |
class MyComponent extends React.Component { | |
render() { | |
const { value, handleChange } = this.props; | |
return <input value={value} onChange={handleChange.bind(this)} /> | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 修正例1:コンストラクタ内でbind関数を使用して、Thisを束縛 | |
class MyComponent extends React.Component { | |
constructor(props) { | |
super(props); | |
this.handleChange = this.props.handleChange.bind(this); | |
} | |
render() { | |
const { value, handleChange } = this.props; | |
return <input value={value} onChange={handleChange} /> | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 修正例2:クラス・プロパティとしてLambdaを使って関数を定義し、Thisを束縛 | |
class MyComponent extends React.Component { | |
handleChange = ev => this.props.handleChange(ev); | |
render() { | |
const { value } = this.props; | |
return <input value={value} onChange={this.handleChange} /> | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 悪い例 | |
class Items extends React.Component { | |
render() { | |
const { items, handleClick } = this.props; | |
return ( | |
<ul> | |
{items.map((item, idx) => <li key={idx} onClick={handleClick.bind(null, item.id)} | |
</ul> | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 修正例 | |
class Item extends React.Component { | |
handleClick() { | |
const { item: { id }, onItemClick } = this.props; | |
onItemClick(id); | |
} | |
render() { | |
const { item: { name } } = this.props; | |
return <li onClick={this.handleClick}>{name}</li>; | |
} | |
} | |
class Items extends React.Component { | |
render() { | |
const { items, handleClick } = this.props; | |
return ( | |
<ul> | |
{items.map((item, idx) => <Item item={item} onItemClick={handleClick} />} | |
</ul> | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 関数式のRef | |
class MyComponent extends React.Component { | |
render() { | |
return (<button ref={(el) => this.buttonElement = el}>Click Me!</button>); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Refハンドラ関数を外出(TypeScriptで書いてます) | |
class MyComponent extends React.Component<P, S> { | |
private refHandlers: any = { | |
button: (ref: HTMLButtonElement): void => { this.buttonElement = ref; }, | |
}; | |
render() { | |
return (<button ref={this.refHandlers.button}>Click Me!</button>); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment