2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

atomでReactのコンポーネント新規作成用snippetを登録してみた

2
Last updated at Posted at 2016-10-05

ReactでES2015のクラス定義でコンポーネントを作成することが多いのだけど、いかんせん記述量が結構あるので毎回辛いなと思っていた。
そういえばatomはsnippet登録出来るはずだよなと思い調べてみました。

結論 ~/.atom/snippets.cson に書くだけ

ターミナルで atom ~/.atom/snippets.cson してファイルを開いてsnippetを追加すれば、エディタの再起動も必要なくすぐ使えた。

登録したsnippetはこちらです

'.source.jsx':
  'Component Skelton':
    'prefix': 'rcs'
    'body': """
      import React, { Component, PropTypes } from "react";

      export default class ${1:YourComponentName} extends Component {
        static get componentName() {
          return "${1:YourComponentName}";
        }

        static get propTypes() {
          return {
          };
        }

        constructor(props) {
          super(props);
          this.state = {
          };
        }

        render() {
          return (
            <div>
            </div>
          );
        }
      }
    ""

※ __R__eact __C__omponent __S__kelton の略で rcs にしました。

これで hoge.jsx を開いているときに rcs と入力すると

スクリーンショット 2016-10-05 13.01.55.png

というメニューが現れるのでtabキーを押すと

スクリーンショット 2016-10-05 13.02.06.png

こんな感じでスニペットが記述されていくらか幸せになれます。

余談 static get componentName() ってなに

railsのフロントでreactを使うことが多いのだけど、htmlに <span data-component="YourComponentName"></span> とだけ書いておいて、
application.jsで YourComponentName.componentName() に一致するnode全部に自動でマウントするということをやっています。

最初は単純に Function.name を使って紐付けてたんですが、
minifyすると正しく動かなくなってしまうのでこうしています。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?