2016-01-06 05:21:55 +00:00
#+TITLE : React layer
2015-10-30 11:20:58 +00:00
2019-05-07 08:21:07 +00:00
#+TAGS : framework|layer|programming
2019-05-02 21:49:30 +00:00
2015-10-30 11:20:58 +00:00
[[file:img/react.png ]]
2019-05-07 20:05:06 +00:00
* Table of Contents :TOC_5_gh:noexport:
2017-05-22 14:16:12 +00:00
- [[#description ][Description ]]
2017-08-06 02:14:10 +00:00
- [[#features ][Features: ]]
2017-05-22 14:16:12 +00:00
- [[#install ][Install ]]
- [[#optional-configuration ][Optional Configuration ]]
2018-12-05 03:03:03 +00:00
- [[#key-bindings ][Key bindings ]]
2018-04-26 19:43:50 +00:00
- [[#rjsx-mode ][rjsx-mode ]]
2017-05-22 14:16:12 +00:00
- [[#formatting-web-beautify ][Formatting (web-beautify) ]]
- [[#documentation-js-doc ][Documentation (js-doc) ]]
2020-07-02 23:18:40 +00:00
- [[#documentation-tern ][Documentation (tern) ]]
2015-10-30 11:20:58 +00:00
* Description
2015-07-24 20:07:03 +00:00
ES6 and JSX ready configuration layer for React
2018-04-26 19:43:50 +00:00
It will automatically recognize =.jsx= files and files with =react= imported.
2016-02-08 14:54:28 +00:00
2017-08-06 02:14:10 +00:00
** Features:
2015-07-24 20:07:03 +00:00
- on-the-fly syntax checking
- proper syntax highlight and indentation with jsx
2018-04-26 19:43:50 +00:00
- backend support for autocompletion as in rjsx-mode
2015-07-27 22:26:47 +00:00
- jsfmt automatic formatting
- js2-refactor
- js-doc
2015-07-24 20:07:03 +00:00
2015-10-30 11:20:58 +00:00
* Install
2016-01-06 05:21:55 +00:00
To use this configuration layer, add it to your =~/.spacemacs= . You will need to
add =react= to the existing =dotspacemacs-configuration-layers= list in this
file.
2015-07-24 20:07:03 +00:00
2018-04-26 19:43:50 +00:00
React layer uses the same backend defined in javascript layer. Options are =tern= and =lsp= .
2015-07-24 20:07:03 +00:00
2019-04-13 01:59:47 +00:00
React layer also uses the same import backend defined in javascript layer.
2017-05-15 02:20:00 +00:00
To use the on-the-fly syntax checking, install =eslint= with babel and react
support:
2018-07-10 10:57:25 +00:00
2015-07-24 20:07:03 +00:00
#+BEGIN_SRC sh
$ npm install -g eslint babel-eslint eslint-plugin-react
#+END_SRC
2017-05-15 02:20:00 +00:00
If your project do not use a custom =.eslintrc= file I strongly advice you to
try out this one by Airbnb:
2015-07-24 20:07:03 +00:00
[[https://github.com/airbnb/javascript/blob/master/linters/.eslintrc ][.eslintrc ]]
2019-04-13 07:12:06 +00:00
When =lsp= is set as the backend, but you don't want to use lsp as the linter,
set the variable =javascript-lsp-linter= to =nil= in the Javascript layer.
2018-06-19 06:25:33 +00:00
React layer uses the same formatter defined in javascript layer. Options are
=web-beautify= and =prettier= .
To use automatic code formatting you need to install ~js-beautify~ or ~prettier~
2017-05-15 02:20:00 +00:00
with:
2018-07-10 10:57:25 +00:00
2015-07-27 22:26:47 +00:00
#+BEGIN_SRC sh
2018-06-19 06:25:33 +00:00
$ npm install -g js-beautify prettier
2015-07-28 22:05:16 +00:00
#+END_SRC
2017-05-15 02:20:00 +00:00
If you install these in non-standard locations, then add the following to your
=dotspacemacs/user-init= function:
2018-07-10 10:57:25 +00:00
2016-10-15 18:54:05 +00:00
#+BEGIN_SRC elisp
(add-to-list 'exec-path "/path/to/node/bins" t)
#+END_SRC
2017-05-15 02:20:00 +00:00
Be sure to have the ~e4x~ option set to ~true~ on your ~.jsbeautifyrc~ here it
is my configuration as an example:
2018-07-10 10:57:25 +00:00
2015-07-28 22:05:16 +00:00
#+BEGIN_SRC json
{
"indent_size": 2,
"indent_char": " ",
"eol": "\n",
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 2,
"jslint_happy": false,
"space_after_anon_function": false,
"brace_style": "collapse",
"keep_array_indentation": false,
"keep_function_indentation": false,
"space_before_conditional": true,
"break_chained_methods": true,
"eval_code": false,
"unescape_strings": false,
"wrap_line_length": 80,
"wrap_attributes": "auto",
"wrap_attributes_indent_size": 2,
"e4x": true,
2018-08-15 21:16:02 +00:00
"end_with_newline": true,
"brace_style": "collapse-preserve-inline"
2015-07-28 22:05:16 +00:00
}
2015-07-27 22:26:47 +00:00
#+END_SRC
2015-10-30 11:20:58 +00:00
* Optional Configuration
2017-05-15 02:20:00 +00:00
You may refer to the =web-mode= configuration for fine tuning the indenting
behaviour.
2015-07-24 20:07:03 +00:00
2017-05-15 02:20:00 +00:00
For example to have a consistent 2 spaces indenting both on =js= and =jsx= you
may use these settings:
2015-07-24 20:07:03 +00:00
2018-09-19 03:54:47 +00:00
#+BEGIN_SRC emacs-lisp
2015-07-24 20:07:03 +00:00
(setq-default
;; js2-mode
js2-basic-offset 2
;; web-mode
css-indent-offset 2
web-mode-markup-indent-offset 2
web-mode-css-indent-offset 2
web-mode-code-indent-offset 2
web-mode-attr-indent-offset 2)
2018-09-19 03:54:47 +00:00
#+END_SRC
2015-07-24 20:07:03 +00:00
2019-05-26 20:58:52 +00:00
And if you want to have 2 space indent also for element's attributes,
2017-05-15 02:20:00 +00:00
concatenations and contiguous function calls:
2018-07-10 10:57:25 +00:00
2018-09-19 03:54:47 +00:00
#+BEGIN_SRC emacs-lisp
2015-07-24 20:07:03 +00:00
(with-eval-after-load 'web-mode
(add-to-list 'web-mode-indentation-params '("lineup-args" . nil))
(add-to-list 'web-mode-indentation-params '("lineup-concats" . nil))
(add-to-list 'web-mode-indentation-params '("lineup-calls" . nil)))
2018-09-19 03:54:47 +00:00
#+END_SRC
2015-07-24 20:07:03 +00:00
2018-12-05 03:03:03 +00:00
* Key bindings
2018-04-26 19:43:50 +00:00
** rjsx-mode
2020-11-27 19:18:09 +00:00
The =javascript= layers keymap is set as the parent of the =react= layers keymap.
Check the [[https://github.com/syl20bnr/spacemacs/blob/develop/layers/+lang/javascript/README.org#key-bindings ][javascript layers documentation ]] for additional key bindings.
2018-05-18 05:58:00 +00:00
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2018-04-26 19:43:50 +00:00
|---------------+----------------------------------------------------------------------------------|
| ~<~ | inserts </> whenever it would start a new JSX node |
| ~>~ | right before the slash in a self-closing tag automatically inserts a closing tag |
2020-04-10 17:30:03 +00:00
| ~SPC m r l t~ | log thing at point |
2018-04-26 19:43:50 +00:00
| ~SPC m r r t~ | rename tag at point |
2015-07-28 22:05:16 +00:00
** Formatting (web-beautify)
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2015-07-28 22:05:16 +00:00
|-------------+--------------------------------------------------------------|
| ~SPC m =~ | beautify code in js2-mode, json-mode, web-mode, and css-mode |
*** Documentation (js-doc)
2015-07-27 22:26:47 +00:00
You can check more [[https://github.com/mooz/js-doc/ ][here ]]
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2015-07-27 22:26:47 +00:00
|---------------+---------------------------------------|
2015-07-28 22:05:16 +00:00
| ~SPC m r d b~ | insert JSDoc comment for current file |
| ~SPC m r d f~ | insert JSDoc comment for function |
| ~SPC m r d t~ | insert tag to comment |
| ~SPC m r d h~ | show list of available jsdoc tags |
2015-07-27 22:26:47 +00:00
2020-07-02 23:18:40 +00:00
** Documentation (tern)
2015-07-27 22:26:47 +00:00
2018-12-05 03:03:03 +00:00
| Key binding | Description |
2015-07-27 22:26:47 +00:00
|---------------+------------------------------------------------------------------------------------------|
| ~SPC m C-g~ | brings you back to last place you were when you pressed M-.. |
| ~SPC m g g~ | jump to the definition of the thing under the cursor |
| ~SPC m g G~ | jump to definition for the given name |
| ~SPC m h d~ | find docs of the thing under the cursor. Press again to open the associated URL (if any) |
| ~SPC m h t~ | find the type of the thing under the cursor |
2015-07-28 22:05:16 +00:00
| ~SPC m r r V~ | rename variable under the cursor using tern |