Destructuring props assignment

Destructuring props assignment



I'm having some issues with eslint in react. Its asking me to use destructuring props assignment but when I change my code, it breaks.



Any ideas?



This is the full code


class LoginPage extends React.Component
submit = data =>

// This is how I tried to fix it!
//
// const login, history = this.props;
// login(data).then(() => history.push('/'));
// ;

// This is what I have, its working but eslint is complaining.
this.props.login(data).then(() => this.props.history.push('/'));

render()
return (
<div>
<h1>Login Page</h1>
<LoginForm submit=this.submit />
</div>
);



LoginPage.propTypes =
history: PropTypes.shape(
push: PropTypes.func.isRequired
).isRequired,
login: PropTypes.func.isRequired /* eslint-disable-line */
;

export default connect(
null,
login
)(LoginPage);



The error I get with the modified code is:


TypeError: Object(...)(...).then is not a function
LoginPage._this.submit
src/ components/pages/LoginPage.js:10

7 | class LoginPage extends React.Component {
8 | submit = data =>
9 ;
12 |
13 | render() {



It says the problem is in line 10.



Any help would be appreciated!




3 Answers
3



When you have multiple expression within a function, you need to write it within . In your case you would write



submit = data =>

const login, history = this.props;
login(data).then(() => history.push('/'));






Yes, sorry I forgot to add that to the code. I used curly braces in my solution at first.
– David
Aug 29 at 8:10





What is the error that you get
– Shubham Khatri
Aug 29 at 8:10





I edited my post with the error message.
– David
Aug 29 at 8:28



Your fix looks fine, have you tried putting submit = (data) => ... in curly braces? Unless you're using something like coffeescript, multiple statements (one destructuring assignment and then the call) need to be in a block. That might be why eslint complains but it's working otherwise - it's just one expression, so no block necessary.


submit = (data) => ...



As the other point out you need curly braces . Like the following:



submit = data =>

const login, history = this.props;
login(data).then(() => history.push('/'));




es6 arrow function works with the following rules:


es6



return






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ