admin管理员组文章数量:1415139
I'm currently creating a ponent in react and i'm using the ES Lint rule react/jsx-no-bind
. My issue here is that I want to be able to pass a parameter to my ponents function. Here is the code I would like to use to be able to do so:
class LanguageDropdown extends Component {
constructor(props) {
super(props);
this.state = {};
}
changeLanguage = (lang) => {
console.log(lang)
};
render() {
return (
<div>
{this.props.languages.map(lang => <button onCLick={() => this.changeLanguage(lang)}>{lang}</button>)}
</div>
)
}
...
This pulls up the ESlint error:
JSX props should not use arrow functions
I'm not entirely sure how to achieve this without using an arrow function or using .bind()
. I could add a data-attribute to the button element and then just pass in the event into the changeLanguage
function and fetch the attribute using event.target() but this doesn't feel like it's the way it should be approached in React.
Can someone tell me what would be the correct way?
I'm currently creating a ponent in react and i'm using the ES Lint rule react/jsx-no-bind
. My issue here is that I want to be able to pass a parameter to my ponents function. Here is the code I would like to use to be able to do so:
class LanguageDropdown extends Component {
constructor(props) {
super(props);
this.state = {};
}
changeLanguage = (lang) => {
console.log(lang)
};
render() {
return (
<div>
{this.props.languages.map(lang => <button onCLick={() => this.changeLanguage(lang)}>{lang}</button>)}
</div>
)
}
...
This pulls up the ESlint error:
JSX props should not use arrow functions
I'm not entirely sure how to achieve this without using an arrow function or using .bind()
. I could add a data-attribute to the button element and then just pass in the event into the changeLanguage
function and fetch the attribute using event.target() but this doesn't feel like it's the way it should be approached in React.
Can someone tell me what would be the correct way?
Share Improve this question asked Aug 2, 2018 at 10:33 red house 87red house 87 2,42512 gold badges61 silver badges110 bronze badges 3- take a look at stackoverflow./a/41053846/2823226 – Alireza Commented Aug 2, 2018 at 10:51
-
If I may, why do you use the rule
no-bind
? – ChrisR Commented Aug 2, 2018 at 10:51 - no-bind is part of the es lint config i inherited. I believe its best practice? – red house 87 Commented Aug 2, 2018 at 11:30
2 Answers
Reset to default 2You can refactor button into its own ponent:
class MyButton extends Component {
static propTypes = {
language: PropTypes.string.isRequired,
};
onClick = () => console.log(this.props.language);
render() {
const {language} = this.props;
return (
<button onClick={this.onClick} type="submit">
{language}
</button>);
}
}
and then in your LanguageDropDown class, use MyButton like this:
class LanguageDropdown extends Component {
...
render() {
return (
<div>
{this.props.languages.map(lang => <MyButton key={lang} language={lang}/>)}
</div>
)
}
...
}
A couple of additional things:
- You have a typo onCLick should be onClick
- You need a key for repeated items
try the below code. here I tried by taking the value into the state, same can be tried using props. class LanguageDropdown extends Component { constructor(props) { super(props); this.state = {languages:['telugu','hindi','english']}; // this.changeLanguage = this.changeLanguage.bind(this); }
changeLanguage(event,lang){
//event.preventDefault();
console.log('change lang: '+JSON.stringify(lang));
};
render() {
return (
<div>
{this.state.languages.map(lang => <button onClick={(event)=>this.changeLanguage(event,lang)}>{lang}</button>)}
</div>
)
}
}
render(<LanguageDropdown />, document.getElementById('root'));
when you bind the handler in the onClick event where you are passing the value to the handler, then we have to pass that value from the event and collect it to get that value.
本文标签: javascriptReact ESLintJSX props should not use arrow functionsStack Overflow
版权声明:本文标题:javascript - React ESLint - JSX props should not use arrow functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745213067a2648002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论