what is 'require' function is ReactJS?

后端 未结 2 1378
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 11:22

Take the below sample code for example.

require(\'react-bootstrap-datetimepicker\');

...

render: function() {
  return ;
}
         


        
2条回答
  •  攒了一身酷
    2020-12-19 11:52

    The require function is intended to add separate pieces of code (“modules”) to the current scope, a feature that was not part of the JavaScript/ECMAScript language until the ES2015 specification.

    Therefore this function is not specific to ReactJS, and is not part of the language either, which is why Firefox throws an error when you attempt to use it in a vanilla browser environment.

    Using require to load modules synchronously is generally part of a method known as CommonJS (see this answer for more on module formats). While an environment such as Node.js provides a module API similar to this specification, browsers do not; so you must bring the function yourself.

    There are many options to do so, and it is up to you to pick the one that best suits your workflow and personal taste. But overall, the patterns come down to either:

    • Explicitly use a module loader in the browser: using a
提交回复
热议问题