• <tfoot id='oT8GO'></tfoot>
      <bdo id='oT8GO'></bdo><ul id='oT8GO'></ul>
    <i id='oT8GO'><tr id='oT8GO'><dt id='oT8GO'><q id='oT8GO'><span id='oT8GO'><b id='oT8GO'><form id='oT8GO'><ins id='oT8GO'></ins><ul id='oT8GO'></ul><sub id='oT8GO'></sub></form><legend id='oT8GO'></legend><bdo id='oT8GO'><pre id='oT8GO'><center id='oT8GO'></center></pre></bdo></b><th id='oT8GO'></th></span></q></dt></tr></i><div id='oT8GO'><tfoot id='oT8GO'></tfoot><dl id='oT8GO'><fieldset id='oT8GO'></fieldset></dl></div>

    <small id='oT8GO'></small><noframes id='oT8GO'>

      1. <legend id='oT8GO'><style id='oT8GO'><dir id='oT8GO'><q id='oT8GO'></q></dir></style></legend>
      2. 在 React.js 中拥有像 componentWillMount 这样的函数的目的是什么?

        What is the purpose of having functions like componentWillMount in React.js?(在 React.js 中拥有像 componentWillMount 这样的函数的目的是什么?)
          <tbody id='ZvQLw'></tbody>
            <tfoot id='ZvQLw'></tfoot>
            <legend id='ZvQLw'><style id='ZvQLw'><dir id='ZvQLw'><q id='ZvQLw'></q></dir></style></legend>
              <bdo id='ZvQLw'></bdo><ul id='ZvQLw'></ul>

                <small id='ZvQLw'></small><noframes id='ZvQLw'>

                  <i id='ZvQLw'><tr id='ZvQLw'><dt id='ZvQLw'><q id='ZvQLw'><span id='ZvQLw'><b id='ZvQLw'><form id='ZvQLw'><ins id='ZvQLw'></ins><ul id='ZvQLw'></ul><sub id='ZvQLw'></sub></form><legend id='ZvQLw'></legend><bdo id='ZvQLw'><pre id='ZvQLw'><center id='ZvQLw'></center></pre></bdo></b><th id='ZvQLw'></th></span></q></dt></tr></i><div id='ZvQLw'><tfoot id='ZvQLw'></tfoot><dl id='ZvQLw'><fieldset id='ZvQLw'></fieldset></dl></div>

                  本文介绍了在 React.js 中拥有像 componentWillMount 这样的函数的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我最近一直在用 React.js 编写组件.我从来不用像 componentWillMountcomponentDidMount 这样的方法.

                  I have been writing components in React.js recently. I have never had to use methods like componentWillMount and componentDidMount.

                  render 是必不可少的.我写的 getInitialState 和其他辅助方法也派上用场了.但不是前面提到的两种生命周期方法.

                  render is indispensable. getInitialState and other helper methods I wrote also come in handy. But not the two aforementioned lifecycle methods.

                  我目前的猜测是它们用于调试?我可以在其中 console.log 退出:

                  My current guess is that they are used for debugging? I can console.log out inside them:

                  componentWillMount: function() {
                    console.log('component currently mounting');
                  },
                  
                  componentDidMount: function() {
                    console.log('component has mounted');
                  } 
                  

                  还有其他用途吗?

                  推荐答案

                  componentDidMount 如果你想使用一些非 React JavaScript 插件是很有用的.例如,React 中缺少一个好的日期选择器.Pickaday 很漂亮,而且开箱即用.所以我的 DateRangeInput 组件现在使用 Pickaday 作为开始和结束日期输入,我将它连接起来,如下所示:

                  componentDidMount is useful if you want to use some non-React JavaScript plugins. For example, there is a lack of a good date picker in React. Pickaday is beautiful and it just plain works out of the box. So my DateRangeInput component is now using Pickaday for the start and end date input and I hooked it up like so:

                    componentDidMount: function() {
                      new Pikaday({
                        field: React.findDOMNode(this.refs.start),
                        format: 'MM/DD/YYYY',
                        onSelect: this.onChangeStart
                      });
                  
                      new Pikaday({
                        field: React.findDOMNode(this.refs.end),
                        format: 'MM/DD/YYYY',
                        onSelect: this.onChangeEnd
                      });
                    },
                  

                  需要为 Pikaday 渲染 DOM 以连接到它,而 componentDidMount 钩子可以让您连接到该确切事件.

                  The DOM needs to be rendered for Pikaday to hook up to it and the componentDidMount hook lets you hook into that exact event.

                  componentWillMount 当您想在组件安装之前以编程方式执行某些操作时很有用.我正在处理的一个代码库中的一个示例是一个 mixin,它有一堆代码,否则这些代码会在许多不同的菜单组件中重复.componentWillMount 用于设置一个特定共享属性的状态.componentWillMount 的另一种使用方式是通过 prop(s) 设置组件分支的行为:

                  componentWillMount is useful when you want to do something programatically right before the component mounts. An example in one codebase I'm working on is a mixin that has a bunch of code that would otherwise be duplicated in a number of different menu components. componentWillMount is used to set the state of one specific shared attribute. Another way componentWillMount could be used is to set a behaviour of the component branching by prop(s):

                    componentWillMount() {
                      let mode;
                      if (this.props.age > 70) {
                        mode = 'old';
                      } else if (this.props.age < 18) {
                        mode = 'young';
                      } else {
                        mode = 'middle';
                      }
                      this.setState({ mode });
                    }
                  

                  这篇关于在 React.js 中拥有像 componentWillMount 这样的函数的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)
                  <tfoot id='Zk0PI'></tfoot>

                        <bdo id='Zk0PI'></bdo><ul id='Zk0PI'></ul>
                          <tbody id='Zk0PI'></tbody>
                        <legend id='Zk0PI'><style id='Zk0PI'><dir id='Zk0PI'><q id='Zk0PI'></q></dir></style></legend>
                          <i id='Zk0PI'><tr id='Zk0PI'><dt id='Zk0PI'><q id='Zk0PI'><span id='Zk0PI'><b id='Zk0PI'><form id='Zk0PI'><ins id='Zk0PI'></ins><ul id='Zk0PI'></ul><sub id='Zk0PI'></sub></form><legend id='Zk0PI'></legend><bdo id='Zk0PI'><pre id='Zk0PI'><center id='Zk0PI'></center></pre></bdo></b><th id='Zk0PI'></th></span></q></dt></tr></i><div id='Zk0PI'><tfoot id='Zk0PI'></tfoot><dl id='Zk0PI'><fieldset id='Zk0PI'></fieldset></dl></div>
                          1. <small id='Zk0PI'></small><noframes id='Zk0PI'>