ReactJS: Return value when item is clicked (multi step form)










0















I am fairly new to ReactJS. For a small project I am building, I have created a component which returns a value when a card item is clicked. The goal is to build a multi step form.



So far it works as expected but I am pretty sure I am not following best practices.



Especially the 3 event listeners seems to be to repetitive.



Does someone more experienced has suggestions on how to improve the code? Many thanks for your support. :)



 import React from 'react';
import PropTypes from 'prop-types';
import withStyles from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
import Paper from '@material-ui/core/Paper';

// Styles for grid items/cards
const styles = theme => (
root:
flexGrow: 1,
,
paper:
height: 250,
width: 200,
margin: 20,
,
);

// COMPONENT
export class Form1 extends React.Component
state =
spacing: '0',
step: 1,
optionStep1: ''
;

// METHODS
// Proceed to next step
nextStep = () =>
const step = this.state;
this.setState(
step: step + 1
);


// Previus step
prevStep = () =>
const step = this.state;
this.setState(
step: step - 1
);


// Change Option of Step 1
constructor(props)
super(props);
this.state =
optionStep1: ''
;

// This binding is necessary to make `this` work in the callback
this.handleClick1 = this.handleClick1.bind(this);
this.handleClick2 = this.handleClick2.bind(this);
this.handleClick3 = this.handleClick3.bind(this);


// Eventlisteners
handleClick1()
this.setState(state => (
optionStep1: 'option 1 picked'
)
);
console.log('option 1 picked');


handleClick2()
this.setState(state => (
optionStep1: 'option 2 picked'
)
);
console.log('option 2 picked');


handleClick3()
this.setState(state => (
optionStep1: 'option 3 picked'
)
);
console.log('option 3 picked');


// RENDER
render()
const classes = this.props;
const spacing = this.state;

return (
<Grid container className=classes.root spacing=16>
<Grid item xs=12>
<Grid container className=classes.demo justify="center" spacing=Number(spacing)>
<Grid onClick=this.handleClick1>
<Paper className=classes.paper />
</Grid>
<Grid onClick=this.handleClick2>
<Paper className=classes.paper />
</Grid>
<Grid onClick=this.handleClick3>
<Paper className=classes.paper />
</Grid>
</Grid>
</Grid>
</Grid>
);



Form1.propTypes =
classes: PropTypes.object.isRequired,
;

export default withStyles(styles)(Form1);









share|improve this question

















  • 4





    I'm voting to close this question as off-topic because it is requesting a review of working code. It may be more suitable for Code Review

    – Robbie Averill
    Nov 13 '18 at 10:55















0















I am fairly new to ReactJS. For a small project I am building, I have created a component which returns a value when a card item is clicked. The goal is to build a multi step form.



So far it works as expected but I am pretty sure I am not following best practices.



Especially the 3 event listeners seems to be to repetitive.



Does someone more experienced has suggestions on how to improve the code? Many thanks for your support. :)



 import React from 'react';
import PropTypes from 'prop-types';
import withStyles from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
import Paper from '@material-ui/core/Paper';

// Styles for grid items/cards
const styles = theme => (
root:
flexGrow: 1,
,
paper:
height: 250,
width: 200,
margin: 20,
,
);

// COMPONENT
export class Form1 extends React.Component
state =
spacing: '0',
step: 1,
optionStep1: ''
;

// METHODS
// Proceed to next step
nextStep = () =>
const step = this.state;
this.setState(
step: step + 1
);


// Previus step
prevStep = () =>
const step = this.state;
this.setState(
step: step - 1
);


// Change Option of Step 1
constructor(props)
super(props);
this.state =
optionStep1: ''
;

// This binding is necessary to make `this` work in the callback
this.handleClick1 = this.handleClick1.bind(this);
this.handleClick2 = this.handleClick2.bind(this);
this.handleClick3 = this.handleClick3.bind(this);


// Eventlisteners
handleClick1()
this.setState(state => (
optionStep1: 'option 1 picked'
)
);
console.log('option 1 picked');


handleClick2()
this.setState(state => (
optionStep1: 'option 2 picked'
)
);
console.log('option 2 picked');


handleClick3()
this.setState(state => (
optionStep1: 'option 3 picked'
)
);
console.log('option 3 picked');


// RENDER
render()
const classes = this.props;
const spacing = this.state;

return (
<Grid container className=classes.root spacing=16>
<Grid item xs=12>
<Grid container className=classes.demo justify="center" spacing=Number(spacing)>
<Grid onClick=this.handleClick1>
<Paper className=classes.paper />
</Grid>
<Grid onClick=this.handleClick2>
<Paper className=classes.paper />
</Grid>
<Grid onClick=this.handleClick3>
<Paper className=classes.paper />
</Grid>
</Grid>
</Grid>
</Grid>
);



Form1.propTypes =
classes: PropTypes.object.isRequired,
;

export default withStyles(styles)(Form1);









share|improve this question

















  • 4





    I'm voting to close this question as off-topic because it is requesting a review of working code. It may be more suitable for Code Review

    – Robbie Averill
    Nov 13 '18 at 10:55













0












0








0








I am fairly new to ReactJS. For a small project I am building, I have created a component which returns a value when a card item is clicked. The goal is to build a multi step form.



So far it works as expected but I am pretty sure I am not following best practices.



Especially the 3 event listeners seems to be to repetitive.



Does someone more experienced has suggestions on how to improve the code? Many thanks for your support. :)



 import React from 'react';
import PropTypes from 'prop-types';
import withStyles from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
import Paper from '@material-ui/core/Paper';

// Styles for grid items/cards
const styles = theme => (
root:
flexGrow: 1,
,
paper:
height: 250,
width: 200,
margin: 20,
,
);

// COMPONENT
export class Form1 extends React.Component
state =
spacing: '0',
step: 1,
optionStep1: ''
;

// METHODS
// Proceed to next step
nextStep = () =>
const step = this.state;
this.setState(
step: step + 1
);


// Previus step
prevStep = () =>
const step = this.state;
this.setState(
step: step - 1
);


// Change Option of Step 1
constructor(props)
super(props);
this.state =
optionStep1: ''
;

// This binding is necessary to make `this` work in the callback
this.handleClick1 = this.handleClick1.bind(this);
this.handleClick2 = this.handleClick2.bind(this);
this.handleClick3 = this.handleClick3.bind(this);


// Eventlisteners
handleClick1()
this.setState(state => (
optionStep1: 'option 1 picked'
)
);
console.log('option 1 picked');


handleClick2()
this.setState(state => (
optionStep1: 'option 2 picked'
)
);
console.log('option 2 picked');


handleClick3()
this.setState(state => (
optionStep1: 'option 3 picked'
)
);
console.log('option 3 picked');


// RENDER
render()
const classes = this.props;
const spacing = this.state;

return (
<Grid container className=classes.root spacing=16>
<Grid item xs=12>
<Grid container className=classes.demo justify="center" spacing=Number(spacing)>
<Grid onClick=this.handleClick1>
<Paper className=classes.paper />
</Grid>
<Grid onClick=this.handleClick2>
<Paper className=classes.paper />
</Grid>
<Grid onClick=this.handleClick3>
<Paper className=classes.paper />
</Grid>
</Grid>
</Grid>
</Grid>
);



Form1.propTypes =
classes: PropTypes.object.isRequired,
;

export default withStyles(styles)(Form1);









share|improve this question














I am fairly new to ReactJS. For a small project I am building, I have created a component which returns a value when a card item is clicked. The goal is to build a multi step form.



So far it works as expected but I am pretty sure I am not following best practices.



Especially the 3 event listeners seems to be to repetitive.



Does someone more experienced has suggestions on how to improve the code? Many thanks for your support. :)



 import React from 'react';
import PropTypes from 'prop-types';
import withStyles from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
import Paper from '@material-ui/core/Paper';

// Styles for grid items/cards
const styles = theme => (
root:
flexGrow: 1,
,
paper:
height: 250,
width: 200,
margin: 20,
,
);

// COMPONENT
export class Form1 extends React.Component
state =
spacing: '0',
step: 1,
optionStep1: ''
;

// METHODS
// Proceed to next step
nextStep = () =>
const step = this.state;
this.setState(
step: step + 1
);


// Previus step
prevStep = () =>
const step = this.state;
this.setState(
step: step - 1
);


// Change Option of Step 1
constructor(props)
super(props);
this.state =
optionStep1: ''
;

// This binding is necessary to make `this` work in the callback
this.handleClick1 = this.handleClick1.bind(this);
this.handleClick2 = this.handleClick2.bind(this);
this.handleClick3 = this.handleClick3.bind(this);


// Eventlisteners
handleClick1()
this.setState(state => (
optionStep1: 'option 1 picked'
)
);
console.log('option 1 picked');


handleClick2()
this.setState(state => (
optionStep1: 'option 2 picked'
)
);
console.log('option 2 picked');


handleClick3()
this.setState(state => (
optionStep1: 'option 3 picked'
)
);
console.log('option 3 picked');


// RENDER
render()
const classes = this.props;
const spacing = this.state;

return (
<Grid container className=classes.root spacing=16>
<Grid item xs=12>
<Grid container className=classes.demo justify="center" spacing=Number(spacing)>
<Grid onClick=this.handleClick1>
<Paper className=classes.paper />
</Grid>
<Grid onClick=this.handleClick2>
<Paper className=classes.paper />
</Grid>
<Grid onClick=this.handleClick3>
<Paper className=classes.paper />
</Grid>
</Grid>
</Grid>
</Grid>
);



Form1.propTypes =
classes: PropTypes.object.isRequired,
;

export default withStyles(styles)(Form1);






reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 10:53









Leon HoffmannLeon Hoffmann

11




11







  • 4





    I'm voting to close this question as off-topic because it is requesting a review of working code. It may be more suitable for Code Review

    – Robbie Averill
    Nov 13 '18 at 10:55












  • 4





    I'm voting to close this question as off-topic because it is requesting a review of working code. It may be more suitable for Code Review

    – Robbie Averill
    Nov 13 '18 at 10:55







4




4





I'm voting to close this question as off-topic because it is requesting a review of working code. It may be more suitable for Code Review

– Robbie Averill
Nov 13 '18 at 10:55





I'm voting to close this question as off-topic because it is requesting a review of working code. It may be more suitable for Code Review

– Robbie Averill
Nov 13 '18 at 10:55












1 Answer
1






active

oldest

votes


















0














You are exporting your component twice, you must have gotten an error about this somewhere. You can just write it like this using ES6 arrow functions:



import React, Component from 'react';

class Form1 extends Component
state =
spacing: '0',
step: 1,
optionStep1: ''
;


render()
return (
<div>
<SurveyForm />
</div>
);



export default Form1;


You are making use of the Babel plugin that comes with create-react-app to initialize state, but then down a few lines you go and write out the constructor() in the classical way, initializing state twice.



After you initialize state, if you are changing that state you need to use this.setState(). I would also recommend using redux-form library.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279418%2freactjs-return-value-when-item-is-clicked-multi-step-form%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You are exporting your component twice, you must have gotten an error about this somewhere. You can just write it like this using ES6 arrow functions:



    import React, Component from 'react';

    class Form1 extends Component
    state =
    spacing: '0',
    step: 1,
    optionStep1: ''
    ;


    render()
    return (
    <div>
    <SurveyForm />
    </div>
    );



    export default Form1;


    You are making use of the Babel plugin that comes with create-react-app to initialize state, but then down a few lines you go and write out the constructor() in the classical way, initializing state twice.



    After you initialize state, if you are changing that state you need to use this.setState(). I would also recommend using redux-form library.






    share|improve this answer





























      0














      You are exporting your component twice, you must have gotten an error about this somewhere. You can just write it like this using ES6 arrow functions:



      import React, Component from 'react';

      class Form1 extends Component
      state =
      spacing: '0',
      step: 1,
      optionStep1: ''
      ;


      render()
      return (
      <div>
      <SurveyForm />
      </div>
      );



      export default Form1;


      You are making use of the Babel plugin that comes with create-react-app to initialize state, but then down a few lines you go and write out the constructor() in the classical way, initializing state twice.



      After you initialize state, if you are changing that state you need to use this.setState(). I would also recommend using redux-form library.






      share|improve this answer



























        0












        0








        0







        You are exporting your component twice, you must have gotten an error about this somewhere. You can just write it like this using ES6 arrow functions:



        import React, Component from 'react';

        class Form1 extends Component
        state =
        spacing: '0',
        step: 1,
        optionStep1: ''
        ;


        render()
        return (
        <div>
        <SurveyForm />
        </div>
        );



        export default Form1;


        You are making use of the Babel plugin that comes with create-react-app to initialize state, but then down a few lines you go and write out the constructor() in the classical way, initializing state twice.



        After you initialize state, if you are changing that state you need to use this.setState(). I would also recommend using redux-form library.






        share|improve this answer















        You are exporting your component twice, you must have gotten an error about this somewhere. You can just write it like this using ES6 arrow functions:



        import React, Component from 'react';

        class Form1 extends Component
        state =
        spacing: '0',
        step: 1,
        optionStep1: ''
        ;


        render()
        return (
        <div>
        <SurveyForm />
        </div>
        );



        export default Form1;


        You are making use of the Babel plugin that comes with create-react-app to initialize state, but then down a few lines you go and write out the constructor() in the classical way, initializing state twice.



        After you initialize state, if you are changing that state you need to use this.setState(). I would also recommend using redux-form library.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 12:30

























        answered Nov 13 '18 at 12:23









        DanielDaniel

        1




        1



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279418%2freactjs-return-value-when-item-is-clicked-multi-step-form%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo