How to hide/display separate forms using buttons and redux?












2















I'm new to react and redux (and posting on stack overflow!).



I'd like to hide/display a redux-form based on a button choice.



I have two buttons: Option-A and Option-B.



I followed the redux tutorial exactly to have their onClick methods dispatch setVisibilityFilter(buttonprops.filter) through a container. See: FilterLink.js This works fine and updates the state's visibilityFilter with the corresponding option.



However, I'm stuck about how I should access the state's filter to hide/display different forms. I would like something similar to what formValueSelector does, but it isn't applicable for buttons (because they don't return values?)



This is my main component's code:



class MainForm extends Component {
render() {
const { error } = this.props
return (
<Grid.Column width={9}>
<Button.Group floated='right'>
<FilterLink filter={VisibilityFilters.SHOW_A}>A</FilterLink>
<Button.Or />
<FilterForm filter={VisibilityFilters.SHOW_B}>B</FilterLink>
</Button.Group>
/* If SHOW_A, display FORM_A, else if SHOW_B, display FORM_B */
</Grid.Column>
)
}}


I feel like just toying with the state directly now would waste the effort of implementing redux. I think I should be passing the value as a prop down to the child forms, but I'm confused how to do so, especially because I don't know how I would get that value without changing my onClick anyway, and onClick is already defined in FilterLink.js



There must be some way to access my state visibility filter to hide/display a form, just unsure how to get there. Thank you!










share|improve this question



























    2















    I'm new to react and redux (and posting on stack overflow!).



    I'd like to hide/display a redux-form based on a button choice.



    I have two buttons: Option-A and Option-B.



    I followed the redux tutorial exactly to have their onClick methods dispatch setVisibilityFilter(buttonprops.filter) through a container. See: FilterLink.js This works fine and updates the state's visibilityFilter with the corresponding option.



    However, I'm stuck about how I should access the state's filter to hide/display different forms. I would like something similar to what formValueSelector does, but it isn't applicable for buttons (because they don't return values?)



    This is my main component's code:



    class MainForm extends Component {
    render() {
    const { error } = this.props
    return (
    <Grid.Column width={9}>
    <Button.Group floated='right'>
    <FilterLink filter={VisibilityFilters.SHOW_A}>A</FilterLink>
    <Button.Or />
    <FilterForm filter={VisibilityFilters.SHOW_B}>B</FilterLink>
    </Button.Group>
    /* If SHOW_A, display FORM_A, else if SHOW_B, display FORM_B */
    </Grid.Column>
    )
    }}


    I feel like just toying with the state directly now would waste the effort of implementing redux. I think I should be passing the value as a prop down to the child forms, but I'm confused how to do so, especially because I don't know how I would get that value without changing my onClick anyway, and onClick is already defined in FilterLink.js



    There must be some way to access my state visibility filter to hide/display a form, just unsure how to get there. Thank you!










    share|improve this question

























      2












      2








      2








      I'm new to react and redux (and posting on stack overflow!).



      I'd like to hide/display a redux-form based on a button choice.



      I have two buttons: Option-A and Option-B.



      I followed the redux tutorial exactly to have their onClick methods dispatch setVisibilityFilter(buttonprops.filter) through a container. See: FilterLink.js This works fine and updates the state's visibilityFilter with the corresponding option.



      However, I'm stuck about how I should access the state's filter to hide/display different forms. I would like something similar to what formValueSelector does, but it isn't applicable for buttons (because they don't return values?)



      This is my main component's code:



      class MainForm extends Component {
      render() {
      const { error } = this.props
      return (
      <Grid.Column width={9}>
      <Button.Group floated='right'>
      <FilterLink filter={VisibilityFilters.SHOW_A}>A</FilterLink>
      <Button.Or />
      <FilterForm filter={VisibilityFilters.SHOW_B}>B</FilterLink>
      </Button.Group>
      /* If SHOW_A, display FORM_A, else if SHOW_B, display FORM_B */
      </Grid.Column>
      )
      }}


      I feel like just toying with the state directly now would waste the effort of implementing redux. I think I should be passing the value as a prop down to the child forms, but I'm confused how to do so, especially because I don't know how I would get that value without changing my onClick anyway, and onClick is already defined in FilterLink.js



      There must be some way to access my state visibility filter to hide/display a form, just unsure how to get there. Thank you!










      share|improve this question














      I'm new to react and redux (and posting on stack overflow!).



      I'd like to hide/display a redux-form based on a button choice.



      I have two buttons: Option-A and Option-B.



      I followed the redux tutorial exactly to have their onClick methods dispatch setVisibilityFilter(buttonprops.filter) through a container. See: FilterLink.js This works fine and updates the state's visibilityFilter with the corresponding option.



      However, I'm stuck about how I should access the state's filter to hide/display different forms. I would like something similar to what formValueSelector does, but it isn't applicable for buttons (because they don't return values?)



      This is my main component's code:



      class MainForm extends Component {
      render() {
      const { error } = this.props
      return (
      <Grid.Column width={9}>
      <Button.Group floated='right'>
      <FilterLink filter={VisibilityFilters.SHOW_A}>A</FilterLink>
      <Button.Or />
      <FilterForm filter={VisibilityFilters.SHOW_B}>B</FilterLink>
      </Button.Group>
      /* If SHOW_A, display FORM_A, else if SHOW_B, display FORM_B */
      </Grid.Column>
      )
      }}


      I feel like just toying with the state directly now would waste the effort of implementing redux. I think I should be passing the value as a prop down to the child forms, but I'm confused how to do so, especially because I don't know how I would get that value without changing my onClick anyway, and onClick is already defined in FilterLink.js



      There must be some way to access my state visibility filter to hide/display a form, just unsure how to get there. Thank you!







      javascript reactjs redux redux-form






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 18:53









      SandraSandra

      203




      203
























          2 Answers
          2






          active

          oldest

          votes


















          0














          With connect, you can pass anything from the Redux Store to your component through its props.



          So based on the link you posted, this should work:



          import { connect } from 'react-redux'

          class MainForm extends Component {
          render() {
          const { error, visibilityFilter } = this.props
          return (
          <Grid.Column width={9}>
          <Button.Group floated='right'>
          <FilterLink filter={VisibilityFilters.SHOW_A}>A</FilterLink>
          <Button.Or />
          <FilterForm filter={VisibilityFilters.SHOW_B}>B</FilterLink>
          </Button.Group>
          {visibilityFilter === VisibilityFilters.SHOW_A
          ? <FormA />
          : <FormB />
          }
          </Grid.Column>
          )
          }}

          const mapStateToProps = state => ({
          visibilityFilter: state.visibilityFilter
          })

          export default connect(mapStateToProps)(MainForm)





          share|improve this answer


























          • sniff Thank you! It works excellently :)

            – Sandra
            Nov 20 '18 at 20:54











          • @Sandra You can upvote and accept the answer with the buttons on the left of this answer. :)

            – GG.
            Nov 20 '18 at 21:52



















          0














          Make sure you have connected the component you want to conditionally render things to the redux store.



          import { connect } from 'react-redux'

          ...

          const mapStateToProps = state => ({visibleFilter: state.visibilityFilter})
          export default connect(mapStateToProps)(MainForm)


          Then you can access this information in your connected component's props, e.g.



          render() {

          return {

          {this.props.visibleFilter === VisibilityFilters.SHOW_A && (<FormA /> )}

          {this.props.visibleFilter === VisibilityFilters.SHOW_B && (<FormB /> )}

          }

          }





          share|improve this answer


























          • Thank you! :) actually now I have a separate issue.. after clicking a button, it'll display the right form, but both buttons become nonfunctional. All the focus seems to be on the form, and the buttons are stuck in their state unless I refresh the page. It seems like the issue is around using redux-form, because if I try displaying normal fields like input, the top buttons still work normally, unlike how they freeze when I display a redux-form after.

            – Sandra
            Nov 20 '18 at 21:51













          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%2f53399684%2fhow-to-hide-display-separate-forms-using-buttons-and-redux%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          With connect, you can pass anything from the Redux Store to your component through its props.



          So based on the link you posted, this should work:



          import { connect } from 'react-redux'

          class MainForm extends Component {
          render() {
          const { error, visibilityFilter } = this.props
          return (
          <Grid.Column width={9}>
          <Button.Group floated='right'>
          <FilterLink filter={VisibilityFilters.SHOW_A}>A</FilterLink>
          <Button.Or />
          <FilterForm filter={VisibilityFilters.SHOW_B}>B</FilterLink>
          </Button.Group>
          {visibilityFilter === VisibilityFilters.SHOW_A
          ? <FormA />
          : <FormB />
          }
          </Grid.Column>
          )
          }}

          const mapStateToProps = state => ({
          visibilityFilter: state.visibilityFilter
          })

          export default connect(mapStateToProps)(MainForm)





          share|improve this answer


























          • sniff Thank you! It works excellently :)

            – Sandra
            Nov 20 '18 at 20:54











          • @Sandra You can upvote and accept the answer with the buttons on the left of this answer. :)

            – GG.
            Nov 20 '18 at 21:52
















          0














          With connect, you can pass anything from the Redux Store to your component through its props.



          So based on the link you posted, this should work:



          import { connect } from 'react-redux'

          class MainForm extends Component {
          render() {
          const { error, visibilityFilter } = this.props
          return (
          <Grid.Column width={9}>
          <Button.Group floated='right'>
          <FilterLink filter={VisibilityFilters.SHOW_A}>A</FilterLink>
          <Button.Or />
          <FilterForm filter={VisibilityFilters.SHOW_B}>B</FilterLink>
          </Button.Group>
          {visibilityFilter === VisibilityFilters.SHOW_A
          ? <FormA />
          : <FormB />
          }
          </Grid.Column>
          )
          }}

          const mapStateToProps = state => ({
          visibilityFilter: state.visibilityFilter
          })

          export default connect(mapStateToProps)(MainForm)





          share|improve this answer


























          • sniff Thank you! It works excellently :)

            – Sandra
            Nov 20 '18 at 20:54











          • @Sandra You can upvote and accept the answer with the buttons on the left of this answer. :)

            – GG.
            Nov 20 '18 at 21:52














          0












          0








          0







          With connect, you can pass anything from the Redux Store to your component through its props.



          So based on the link you posted, this should work:



          import { connect } from 'react-redux'

          class MainForm extends Component {
          render() {
          const { error, visibilityFilter } = this.props
          return (
          <Grid.Column width={9}>
          <Button.Group floated='right'>
          <FilterLink filter={VisibilityFilters.SHOW_A}>A</FilterLink>
          <Button.Or />
          <FilterForm filter={VisibilityFilters.SHOW_B}>B</FilterLink>
          </Button.Group>
          {visibilityFilter === VisibilityFilters.SHOW_A
          ? <FormA />
          : <FormB />
          }
          </Grid.Column>
          )
          }}

          const mapStateToProps = state => ({
          visibilityFilter: state.visibilityFilter
          })

          export default connect(mapStateToProps)(MainForm)





          share|improve this answer















          With connect, you can pass anything from the Redux Store to your component through its props.



          So based on the link you posted, this should work:



          import { connect } from 'react-redux'

          class MainForm extends Component {
          render() {
          const { error, visibilityFilter } = this.props
          return (
          <Grid.Column width={9}>
          <Button.Group floated='right'>
          <FilterLink filter={VisibilityFilters.SHOW_A}>A</FilterLink>
          <Button.Or />
          <FilterForm filter={VisibilityFilters.SHOW_B}>B</FilterLink>
          </Button.Group>
          {visibilityFilter === VisibilityFilters.SHOW_A
          ? <FormA />
          : <FormB />
          }
          </Grid.Column>
          )
          }}

          const mapStateToProps = state => ({
          visibilityFilter: state.visibilityFilter
          })

          export default connect(mapStateToProps)(MainForm)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 21:51

























          answered Nov 20 '18 at 19:07









          GG.GG.

          12k951101




          12k951101













          • sniff Thank you! It works excellently :)

            – Sandra
            Nov 20 '18 at 20:54











          • @Sandra You can upvote and accept the answer with the buttons on the left of this answer. :)

            – GG.
            Nov 20 '18 at 21:52



















          • sniff Thank you! It works excellently :)

            – Sandra
            Nov 20 '18 at 20:54











          • @Sandra You can upvote and accept the answer with the buttons on the left of this answer. :)

            – GG.
            Nov 20 '18 at 21:52

















          sniff Thank you! It works excellently :)

          – Sandra
          Nov 20 '18 at 20:54





          sniff Thank you! It works excellently :)

          – Sandra
          Nov 20 '18 at 20:54













          @Sandra You can upvote and accept the answer with the buttons on the left of this answer. :)

          – GG.
          Nov 20 '18 at 21:52





          @Sandra You can upvote and accept the answer with the buttons on the left of this answer. :)

          – GG.
          Nov 20 '18 at 21:52













          0














          Make sure you have connected the component you want to conditionally render things to the redux store.



          import { connect } from 'react-redux'

          ...

          const mapStateToProps = state => ({visibleFilter: state.visibilityFilter})
          export default connect(mapStateToProps)(MainForm)


          Then you can access this information in your connected component's props, e.g.



          render() {

          return {

          {this.props.visibleFilter === VisibilityFilters.SHOW_A && (<FormA /> )}

          {this.props.visibleFilter === VisibilityFilters.SHOW_B && (<FormB /> )}

          }

          }





          share|improve this answer


























          • Thank you! :) actually now I have a separate issue.. after clicking a button, it'll display the right form, but both buttons become nonfunctional. All the focus seems to be on the form, and the buttons are stuck in their state unless I refresh the page. It seems like the issue is around using redux-form, because if I try displaying normal fields like input, the top buttons still work normally, unlike how they freeze when I display a redux-form after.

            – Sandra
            Nov 20 '18 at 21:51


















          0














          Make sure you have connected the component you want to conditionally render things to the redux store.



          import { connect } from 'react-redux'

          ...

          const mapStateToProps = state => ({visibleFilter: state.visibilityFilter})
          export default connect(mapStateToProps)(MainForm)


          Then you can access this information in your connected component's props, e.g.



          render() {

          return {

          {this.props.visibleFilter === VisibilityFilters.SHOW_A && (<FormA /> )}

          {this.props.visibleFilter === VisibilityFilters.SHOW_B && (<FormB /> )}

          }

          }





          share|improve this answer


























          • Thank you! :) actually now I have a separate issue.. after clicking a button, it'll display the right form, but both buttons become nonfunctional. All the focus seems to be on the form, and the buttons are stuck in their state unless I refresh the page. It seems like the issue is around using redux-form, because if I try displaying normal fields like input, the top buttons still work normally, unlike how they freeze when I display a redux-form after.

            – Sandra
            Nov 20 '18 at 21:51
















          0












          0








          0







          Make sure you have connected the component you want to conditionally render things to the redux store.



          import { connect } from 'react-redux'

          ...

          const mapStateToProps = state => ({visibleFilter: state.visibilityFilter})
          export default connect(mapStateToProps)(MainForm)


          Then you can access this information in your connected component's props, e.g.



          render() {

          return {

          {this.props.visibleFilter === VisibilityFilters.SHOW_A && (<FormA /> )}

          {this.props.visibleFilter === VisibilityFilters.SHOW_B && (<FormB /> )}

          }

          }





          share|improve this answer















          Make sure you have connected the component you want to conditionally render things to the redux store.



          import { connect } from 'react-redux'

          ...

          const mapStateToProps = state => ({visibleFilter: state.visibilityFilter})
          export default connect(mapStateToProps)(MainForm)


          Then you can access this information in your connected component's props, e.g.



          render() {

          return {

          {this.props.visibleFilter === VisibilityFilters.SHOW_A && (<FormA /> )}

          {this.props.visibleFilter === VisibilityFilters.SHOW_B && (<FormB /> )}

          }

          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 19:10









          GG.

          12k951101




          12k951101










          answered Nov 20 '18 at 19:04









          CecilCecil

          22625




          22625













          • Thank you! :) actually now I have a separate issue.. after clicking a button, it'll display the right form, but both buttons become nonfunctional. All the focus seems to be on the form, and the buttons are stuck in their state unless I refresh the page. It seems like the issue is around using redux-form, because if I try displaying normal fields like input, the top buttons still work normally, unlike how they freeze when I display a redux-form after.

            – Sandra
            Nov 20 '18 at 21:51





















          • Thank you! :) actually now I have a separate issue.. after clicking a button, it'll display the right form, but both buttons become nonfunctional. All the focus seems to be on the form, and the buttons are stuck in their state unless I refresh the page. It seems like the issue is around using redux-form, because if I try displaying normal fields like input, the top buttons still work normally, unlike how they freeze when I display a redux-form after.

            – Sandra
            Nov 20 '18 at 21:51



















          Thank you! :) actually now I have a separate issue.. after clicking a button, it'll display the right form, but both buttons become nonfunctional. All the focus seems to be on the form, and the buttons are stuck in their state unless I refresh the page. It seems like the issue is around using redux-form, because if I try displaying normal fields like input, the top buttons still work normally, unlike how they freeze when I display a redux-form after.

          – Sandra
          Nov 20 '18 at 21:51







          Thank you! :) actually now I have a separate issue.. after clicking a button, it'll display the right form, but both buttons become nonfunctional. All the focus seems to be on the form, and the buttons are stuck in their state unless I refresh the page. It seems like the issue is around using redux-form, because if I try displaying normal fields like input, the top buttons still work normally, unlike how they freeze when I display a redux-form after.

          – Sandra
          Nov 20 '18 at 21:51




















          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%2f53399684%2fhow-to-hide-display-separate-forms-using-buttons-and-redux%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?