mary0kinawa
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # React勉強会@Lagoon # introduction ### この勉強会の対象者 --- - React初心者、初めて触ってみる方 - Reactに興味がある方 - ProgateでJavaScriptコースクリアした方 - JavaScriptの基本がわかってる方(class,import,export,アロー関数etc..) <br> ### 今回作成するTODO APPの仕様 --- - Todoを作れる - Todoを一覧表示できる - Todoを削除できる - Todoを編集できる <br> # Step1 - Set Up 1. プロジェクト作成 ``` npx create-react-app react-todo-practice ``` 2. ディレクトリ移動 ``` cd react-todo-practice ``` 3. サーバ起動 ``` npm start ``` ![](https://i.imgur.com/4kQBI98.jpg) 4. Hello,React!を表示してみる 5. デフォルトで入ってる`src`ディレクトリの中身は今回使わないので削除 準備完了╰(*´︶`*)╯♡ <br> # Step2 - index.js と Components を作成する src ディレクトリの中に`index.js` ファイルを作る index.js ```javascript= import React from 'react'; import ReactDOM from 'react-dom'; ReactDOM.render(<div>hoge</div>, document.getElementById('root')); ``` * ReactDOMとはReactで作った要素をHTMLに変換して埋め込んでくれるもの * index.htmlの中の`<div id="root"></div>`の中に埋め込まれる * Appをインポートして第一引数で読みこませる src ディレクトリの中に`components`ディレクトリを作成し、その中に`App.js`ファイルを作る App.js ```javascript= import React from 'react'; class App extends React.Component { render() { return( <div>Hello, React</div> ); } } export default App; ``` index.js に追加、変更する ```javascript= import App from './components/App'; ``` `<div>hoge</div>` を `<App />` に変更する <br> # Step3 - Todoの見た目を作っていく App.js - returnの中身書き換え ```javascript= <React.Fragment> <h1>TODO APP</h1> {/* Form コンポーネント */} <form> <input type="text" /> <button>追加</button> </form> {/* Todo コンポーネント */} <ul> <li> {/* ここから下を command + X でカットする */} 犬の散歩 <button>編集</button> <button>削除</button> {/* ここまで */} </li> <li> {/* ここから下を削除する */} JavaScriptの勉強 <button>編集</button> <button>削除</button> {/* ここまで */} </li> <li> {/* ここから下を削除する */} 腹筋する <button>編集</button> <button>削除</button> {/* ここまで */} </li> </ul> </React.Fragment> ``` <br> # step4 - 各機能をコンポーネント化していく ## Form コンポーネントの作成 1. `Components`の中にForm.js ファイルを作る Form.js ```javascript= import React from 'react'; class Form extends React.Component { render() { return ( <form> <input type="text" /> <button>追加</button> </form> ); } } export default Form; ``` 2. App.js - Form.js をインポートする ```javascript= import Form from './Form'; {/* Form コンポーネント を書き換える */} <Form /> ``` これで最初に作った見た目と変わらないか確認! 変わってなければ、コンポーネント化成功( ̄^ ̄)ゞ 3. Form を動的なものにする Form.js ```javascript= // class Form extends React.Component { の下に追加 constructor(props) { super(props) // this.state の中には初期値が入る this.state = { input: "" }; } ``` 4. form タグの中身を書き換える Form.js ```javascript= <form> <input type="text" value={this.state.input} onChange={this.handleChange} /> <button>追加</button> </form> ``` handleChange メソッドの追加 ```javascript= handleChange = e => { this.setState({ input: e.currentTarget.value }) }; ``` 動的な Form コンポーネントになりました! <br> ## Todo コンポーネントの作成 1. `Components`の中にTodo.js ファイルを作る Todo.js ```javascript= import React from 'react'; class Todo extends React.Component { render() { return ( // ここにJSXを書く ); } } export default Todo; ``` App.js でインポートする ``` import Todo from './Todo'; ``` 2. return の中に App.jsから command + X したコードを貼る Todo.js ```javascript= <React.Fragment> 犬の散歩 <button>編集</button> <button>削除</button> </React.Fragment> ``` ※`React.Fragment` タグで囲うのを忘れずに! 3. TodoにPropsを渡す App.js ```javascript= <li> <Todo id={0} text="犬の散歩" /> </li> ``` 4. Todo.js で Props を反映させる render() { の下に追加する ```javascript= const { text } = this.props ``` 5. "犬の散歩" を `{text}` に変更する 中かっこ→{}で囲うのは、JSX内でJSを使いたい時に{}使用すると反映される 6. 残り2つのTodoもJSに変えましょう! App.js ```javascript= <ul> <li> <Todo id={0} text="犬の散歩" /> </li> <li> <Todo id={0} text="JavaScriptの勉強する" /> </li> <li> <Todo id={0} text="腹筋する" /> </li> </ul> ``` こんな感じでコンポーネントの再利用ができます! では、これを動的なものに変えていきましょう( ̄^ ̄)ゞ <br> ## Todo を追加する実装 1. form タグに onSubmitイベントを指定する Form.js ```javascript= onSubmit={this.handleSubmit} // handleSubmit メソッドを作る handleSubmit = e => { e.preventDefault() if (!this.state.input) return; console.log(this.state.input) }; ``` input の中身を console.log で見てみよう! `e.preventDefault()` とはPostリクエストが起きてページ遷移しないためにするもの 2. state の変更 Form.js console.log を消して以下を追加する ```javascript= this.setState({input: ""}); ``` これで、追加を押すとformの中身が消えるようになったよ٩( 'ω' )و 3. App.js で state を作る App.js 以下のコードを追加 ```javascript= constructor(props) { super(props) // stateの定義 this.state = { todos: [] //todosの初期値の中身は空っぽ }; } ``` `<ul>` の中のコードを以下に変更する ```javascript= {this.state.todos.map(({ id, text }) => { return ( <li key={id}> <Todo text={text} /> </li> ); })} ``` 変数の宣言をする `let currentId = 0;` handleSubmitメソッドを作る ```javascript= handleSubmit = text => { const newTodo = { id: currentId, text, }; const newTodos = [...this.state.todos, newTodo] this.setState({ todos: newTodos }); currentId++; } ``` form に onSubmit イベントを作り、handleSubmitメソッドをわたす ```javascript= onSubmit={this.handleSubmit} ``` Form.js内のhandleSubmitメソッドでpropsを渡す ```javascript= this.props.onSubmit(this.state.input); ``` Todoの追加ができるようになりました!わーい( ´ ▽ ` )ノ <br> # Step5 - Todo を削除する機能の実装 1. Todo.js で `handleClickDelete` メソッドを作る ```javascript= handleClickDelete = () => { const { onDelete, id } = this.props; onDelete(id) } ``` 2. onClickイベントの作成 <button //ここにonClickイベントを追加>削除</button> ```javascript= onClick={this.handleClickDelete} ``` 3. App.js に props を渡す Todo コンポーネントに以下のprops を追加する ```javascript= id={id} onDelete={this.handleClickDelete} ``` 4. `handleClickDelete` メソッドを作る ```javascript= handleClickDelete = id => { const newTodos = this.state.todos.filter(todo => todo.id !== id) this.setState({ todos: newTodos }); }; ``` Todo を削除できるようになったよ!わーい╰(*´︶`*)╯ <br> # Step6 - Todo を編集する機能の実装 1. Todo を作成する属性の中に `editing` を追加する App.js - handleSubmitメソッドのnewTodo の中に以下を追加する。 ```javascript= editing: false ``` 初期値はfalse 2. 編集専用のコンポーネントを作る components ディレクトリの中で `EditTodo.js` コンポーネントを作る EditTodo.js - 以下をコピペする ```javascript= import React from 'react'; class EditTodo extends React.Component { render() { return ( <div>hoge</div> ); } } export default EditTodo; ``` App.js でインポートする `import EditTodo from './EditTodo';` 3. Todo コンポーネントに `editing` を渡す map メソッドの引数に `<li>` の中身を書き換える ```javascript= { editing ? ( <EditTodo text={text} /> ) : ( <Todo id={id} text={text} onDelete={this.handleClickDelete} /> )} ``` 4. 編集ボタンの中に onClickイベントを作る `onClick={this.handleClickEdit}` handleClickEdit メソッドの作成 ```javascript= handleClickEdit = () => { const { onChange, id, editing} = this.props onChange(id, "editing", !editing); } ``` 5. App.js App.js ```javascript= handleClickEdit = (id, key, value) => { const newTodos = this.state.todos.map(todo => { if (todo.id === id) { return { ...todo, [key]: value }; } return todo; }); this.setState({ todos: newTodos }); }; ``` 6. EditTodo.js に state を持たせる EditTodo.js ```javascript= constructor(props) { super(props) this.state = { text: props.text }; } ``` 7. return の中を書き換える ```javascript= <div> <input type="text" value={this.state.text} onChange={this.handleChange} /> </div> ``` 8. handleChange メソッドの作成 ```javascript= handleChange = e => { this.setState({ text: e.currentTarget.value}) } ``` 9. 更新ボタンの作成 `<button onClick={this.handleSubmit}>更新</button>` ```javascript= handleSubmit = () => { const { onSubmit, id } = this.props; if (!this.props.text) return; onSubmit(id, this.state.text); }; ``` 10. App.js の EditTodo に props を渡す ``` id={id} onSubmit={this.handleUpdateTodoText} ``` メソッドの作成 ```javascript= handleUpdateTodoText = (id, text) => { const newTodos = this.state.todos.map(todo => { if (todo.id === id) { return { ...todo, text, editing: false }; } return todo; }); this.setState({ todos: newTodos }); }; ``` 完成しましたー!!!!!! お疲れ様でした╰(*´︶`*)╯♡

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully