Is passing the _setTitle method with the props the way to go?











up vote
2
down vote

favorite












want to update the title present in the Header
everytime we navigate to a new page.
Is passing the _setTitle method with the props the way to go?



import React, { Component } from 'react';
import PropTypes from 'prop-types';


Main App component



export class App extends Component {
constructor() {
super();

this.state = { pageTitle: 'My app' };
this._setTitle = this._setTitle.bind(this);
}

_setTitle(title) {
this.setState({ pageTitle: title });
}

render() {
const { pageTitle } = this.state.pageTitle;

return (
<div>
<Header title={pageTitle} />
{React.cloneElement(children, { setTitle: this._setTitle })}
<Footer />
</div>
);
}
}


Header and Footer Components



export class Header extends Component {
static propTypes = {
title: PropTypes.string
};
// ...
render() {
const { title } = this.props;

return <h2>{title}</h2>;
}
}


export class Footer extends Component {
// Footer code
}


Following are the different page Components:



export class Profile extends Component {
static propTypes = {
setTitle: PropTypes.func.isRequired
};
componentWillMount() {
this.props.setTitle('Profile');
}
}


export class Projects extends Component {
static propTypes = {
setTitle: PropTypes.func.isRequired
};
componentWillMount() {
this.props.setTitle('Projects');
}
// ...
}


export class ProjectForm extends Component {
static propTypes = {
setTitle: PropTypes.func.isRequired
};
componentWillMount() {
this.props.setTitle('New Project');
}
// ...
}


export class Translators extends Component {
static propTypes = {
setTitle: PropTypes.func.isRequired
};
componentWillMount() {
this.props.setTitle('Translators');
}
// ...
}

// ...


How can I improve upon this. I'm new to react so pls suggest If you have any ideas, I'll implement it. Thank you.










share|improve this question









New contributor




Tejas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite












    want to update the title present in the Header
    everytime we navigate to a new page.
    Is passing the _setTitle method with the props the way to go?



    import React, { Component } from 'react';
    import PropTypes from 'prop-types';


    Main App component



    export class App extends Component {
    constructor() {
    super();

    this.state = { pageTitle: 'My app' };
    this._setTitle = this._setTitle.bind(this);
    }

    _setTitle(title) {
    this.setState({ pageTitle: title });
    }

    render() {
    const { pageTitle } = this.state.pageTitle;

    return (
    <div>
    <Header title={pageTitle} />
    {React.cloneElement(children, { setTitle: this._setTitle })}
    <Footer />
    </div>
    );
    }
    }


    Header and Footer Components



    export class Header extends Component {
    static propTypes = {
    title: PropTypes.string
    };
    // ...
    render() {
    const { title } = this.props;

    return <h2>{title}</h2>;
    }
    }


    export class Footer extends Component {
    // Footer code
    }


    Following are the different page Components:



    export class Profile extends Component {
    static propTypes = {
    setTitle: PropTypes.func.isRequired
    };
    componentWillMount() {
    this.props.setTitle('Profile');
    }
    }


    export class Projects extends Component {
    static propTypes = {
    setTitle: PropTypes.func.isRequired
    };
    componentWillMount() {
    this.props.setTitle('Projects');
    }
    // ...
    }


    export class ProjectForm extends Component {
    static propTypes = {
    setTitle: PropTypes.func.isRequired
    };
    componentWillMount() {
    this.props.setTitle('New Project');
    }
    // ...
    }


    export class Translators extends Component {
    static propTypes = {
    setTitle: PropTypes.func.isRequired
    };
    componentWillMount() {
    this.props.setTitle('Translators');
    }
    // ...
    }

    // ...


    How can I improve upon this. I'm new to react so pls suggest If you have any ideas, I'll implement it. Thank you.










    share|improve this question









    New contributor




    Tejas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      want to update the title present in the Header
      everytime we navigate to a new page.
      Is passing the _setTitle method with the props the way to go?



      import React, { Component } from 'react';
      import PropTypes from 'prop-types';


      Main App component



      export class App extends Component {
      constructor() {
      super();

      this.state = { pageTitle: 'My app' };
      this._setTitle = this._setTitle.bind(this);
      }

      _setTitle(title) {
      this.setState({ pageTitle: title });
      }

      render() {
      const { pageTitle } = this.state.pageTitle;

      return (
      <div>
      <Header title={pageTitle} />
      {React.cloneElement(children, { setTitle: this._setTitle })}
      <Footer />
      </div>
      );
      }
      }


      Header and Footer Components



      export class Header extends Component {
      static propTypes = {
      title: PropTypes.string
      };
      // ...
      render() {
      const { title } = this.props;

      return <h2>{title}</h2>;
      }
      }


      export class Footer extends Component {
      // Footer code
      }


      Following are the different page Components:



      export class Profile extends Component {
      static propTypes = {
      setTitle: PropTypes.func.isRequired
      };
      componentWillMount() {
      this.props.setTitle('Profile');
      }
      }


      export class Projects extends Component {
      static propTypes = {
      setTitle: PropTypes.func.isRequired
      };
      componentWillMount() {
      this.props.setTitle('Projects');
      }
      // ...
      }


      export class ProjectForm extends Component {
      static propTypes = {
      setTitle: PropTypes.func.isRequired
      };
      componentWillMount() {
      this.props.setTitle('New Project');
      }
      // ...
      }


      export class Translators extends Component {
      static propTypes = {
      setTitle: PropTypes.func.isRequired
      };
      componentWillMount() {
      this.props.setTitle('Translators');
      }
      // ...
      }

      // ...


      How can I improve upon this. I'm new to react so pls suggest If you have any ideas, I'll implement it. Thank you.










      share|improve this question









      New contributor




      Tejas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      want to update the title present in the Header
      everytime we navigate to a new page.
      Is passing the _setTitle method with the props the way to go?



      import React, { Component } from 'react';
      import PropTypes from 'prop-types';


      Main App component



      export class App extends Component {
      constructor() {
      super();

      this.state = { pageTitle: 'My app' };
      this._setTitle = this._setTitle.bind(this);
      }

      _setTitle(title) {
      this.setState({ pageTitle: title });
      }

      render() {
      const { pageTitle } = this.state.pageTitle;

      return (
      <div>
      <Header title={pageTitle} />
      {React.cloneElement(children, { setTitle: this._setTitle })}
      <Footer />
      </div>
      );
      }
      }


      Header and Footer Components



      export class Header extends Component {
      static propTypes = {
      title: PropTypes.string
      };
      // ...
      render() {
      const { title } = this.props;

      return <h2>{title}</h2>;
      }
      }


      export class Footer extends Component {
      // Footer code
      }


      Following are the different page Components:



      export class Profile extends Component {
      static propTypes = {
      setTitle: PropTypes.func.isRequired
      };
      componentWillMount() {
      this.props.setTitle('Profile');
      }
      }


      export class Projects extends Component {
      static propTypes = {
      setTitle: PropTypes.func.isRequired
      };
      componentWillMount() {
      this.props.setTitle('Projects');
      }
      // ...
      }


      export class ProjectForm extends Component {
      static propTypes = {
      setTitle: PropTypes.func.isRequired
      };
      componentWillMount() {
      this.props.setTitle('New Project');
      }
      // ...
      }


      export class Translators extends Component {
      static propTypes = {
      setTitle: PropTypes.func.isRequired
      };
      componentWillMount() {
      this.props.setTitle('Translators');
      }
      // ...
      }

      // ...


      How can I improve upon this. I'm new to react so pls suggest If you have any ideas, I'll implement it. Thank you.







      reactjs






      share|improve this question









      New contributor




      Tejas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Tejas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 4 at 7:15









      Amin Mozhgani

      5531420




      5531420






      New contributor




      Tejas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 4 at 7:13









      Tejas

      273




      273




      New contributor




      Tejas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Tejas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Tejas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can make use of Context and pass the setTitle method as a Context value, then you can create a Component that has the logic of setting the Title, A simple implementation would look like



          const TitleContext = React.createContext();

          export class App extends Component {
          constructor() {
          super();

          this.state = { pageTitle: 'My app' };
          this._setTitle = this._setTitle.bind(this);
          }

          _setTitle(title) {
          this.setState({ pageTitle: title });
          }

          render() {
          const { pageTitle } = this.state.pageTitle;

          return (
          <TitleContext.Provider value={{setTitle: this._setTitle}}
          <div>
          <Header title={pageTitle} />
          {this.props.children}
          <Footer />
          </div>
          </TitleContext.Provider>
          );
          }
          }

          class TitleSetter extends React.Component {
          static propTypes = {
          title: PropTypes.string.isRequired
          }
          componentDidMount() {
          this.context.setTitle(this.props.title)
          }
          }

          TitleSetter.contextTypes = TitleContext;


          Now in any component you can simply render the TitleSetter like



          export class Profile extends Component {
          render() {
          return (
          <div>
          <TitleSetter title="Profile" />
          {/* other context */}
          </div>
          )
          }
          }


          Also while looking into context, please look at the this question on how to access context outside of render






          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',
            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
            });


            }
            });






            Tejas is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53138578%2fis-passing-the-settitle-method-with-the-props-the-way-to-go%23new-answer', 'question_page');
            }
            );

            Post as a guest
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            You can make use of Context and pass the setTitle method as a Context value, then you can create a Component that has the logic of setting the Title, A simple implementation would look like



            const TitleContext = React.createContext();

            export class App extends Component {
            constructor() {
            super();

            this.state = { pageTitle: 'My app' };
            this._setTitle = this._setTitle.bind(this);
            }

            _setTitle(title) {
            this.setState({ pageTitle: title });
            }

            render() {
            const { pageTitle } = this.state.pageTitle;

            return (
            <TitleContext.Provider value={{setTitle: this._setTitle}}
            <div>
            <Header title={pageTitle} />
            {this.props.children}
            <Footer />
            </div>
            </TitleContext.Provider>
            );
            }
            }

            class TitleSetter extends React.Component {
            static propTypes = {
            title: PropTypes.string.isRequired
            }
            componentDidMount() {
            this.context.setTitle(this.props.title)
            }
            }

            TitleSetter.contextTypes = TitleContext;


            Now in any component you can simply render the TitleSetter like



            export class Profile extends Component {
            render() {
            return (
            <div>
            <TitleSetter title="Profile" />
            {/* other context */}
            </div>
            )
            }
            }


            Also while looking into context, please look at the this question on how to access context outside of render






            share|improve this answer

























              up vote
              0
              down vote



              accepted










              You can make use of Context and pass the setTitle method as a Context value, then you can create a Component that has the logic of setting the Title, A simple implementation would look like



              const TitleContext = React.createContext();

              export class App extends Component {
              constructor() {
              super();

              this.state = { pageTitle: 'My app' };
              this._setTitle = this._setTitle.bind(this);
              }

              _setTitle(title) {
              this.setState({ pageTitle: title });
              }

              render() {
              const { pageTitle } = this.state.pageTitle;

              return (
              <TitleContext.Provider value={{setTitle: this._setTitle}}
              <div>
              <Header title={pageTitle} />
              {this.props.children}
              <Footer />
              </div>
              </TitleContext.Provider>
              );
              }
              }

              class TitleSetter extends React.Component {
              static propTypes = {
              title: PropTypes.string.isRequired
              }
              componentDidMount() {
              this.context.setTitle(this.props.title)
              }
              }

              TitleSetter.contextTypes = TitleContext;


              Now in any component you can simply render the TitleSetter like



              export class Profile extends Component {
              render() {
              return (
              <div>
              <TitleSetter title="Profile" />
              {/* other context */}
              </div>
              )
              }
              }


              Also while looking into context, please look at the this question on how to access context outside of render






              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                You can make use of Context and pass the setTitle method as a Context value, then you can create a Component that has the logic of setting the Title, A simple implementation would look like



                const TitleContext = React.createContext();

                export class App extends Component {
                constructor() {
                super();

                this.state = { pageTitle: 'My app' };
                this._setTitle = this._setTitle.bind(this);
                }

                _setTitle(title) {
                this.setState({ pageTitle: title });
                }

                render() {
                const { pageTitle } = this.state.pageTitle;

                return (
                <TitleContext.Provider value={{setTitle: this._setTitle}}
                <div>
                <Header title={pageTitle} />
                {this.props.children}
                <Footer />
                </div>
                </TitleContext.Provider>
                );
                }
                }

                class TitleSetter extends React.Component {
                static propTypes = {
                title: PropTypes.string.isRequired
                }
                componentDidMount() {
                this.context.setTitle(this.props.title)
                }
                }

                TitleSetter.contextTypes = TitleContext;


                Now in any component you can simply render the TitleSetter like



                export class Profile extends Component {
                render() {
                return (
                <div>
                <TitleSetter title="Profile" />
                {/* other context */}
                </div>
                )
                }
                }


                Also while looking into context, please look at the this question on how to access context outside of render






                share|improve this answer












                You can make use of Context and pass the setTitle method as a Context value, then you can create a Component that has the logic of setting the Title, A simple implementation would look like



                const TitleContext = React.createContext();

                export class App extends Component {
                constructor() {
                super();

                this.state = { pageTitle: 'My app' };
                this._setTitle = this._setTitle.bind(this);
                }

                _setTitle(title) {
                this.setState({ pageTitle: title });
                }

                render() {
                const { pageTitle } = this.state.pageTitle;

                return (
                <TitleContext.Provider value={{setTitle: this._setTitle}}
                <div>
                <Header title={pageTitle} />
                {this.props.children}
                <Footer />
                </div>
                </TitleContext.Provider>
                );
                }
                }

                class TitleSetter extends React.Component {
                static propTypes = {
                title: PropTypes.string.isRequired
                }
                componentDidMount() {
                this.context.setTitle(this.props.title)
                }
                }

                TitleSetter.contextTypes = TitleContext;


                Now in any component you can simply render the TitleSetter like



                export class Profile extends Component {
                render() {
                return (
                <div>
                <TitleSetter title="Profile" />
                {/* other context */}
                </div>
                )
                }
                }


                Also while looking into context, please look at the this question on how to access context outside of render







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 4 at 9:43









                Shubham Khatri

                72.5k1381122




                72.5k1381122






















                    Tejas is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    Tejas is a new contributor. Be nice, and check out our Code of Conduct.













                    Tejas is a new contributor. Be nice, and check out our Code of Conduct.












                    Tejas is a new contributor. Be nice, and check out our Code of Conduct.















                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53138578%2fis-passing-the-settitle-method-with-the-props-the-way-to-go%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest




















































































                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini