前提・実現したいこと
propsの使い方がよく理解できないです。
該当のソースコード
import React from 'react' import ReactDOM from 'react-dom' import PropTypes from 'prop-types' import './index.css' const User = () =>{ const users =[ {name:"Tom",age:"20",adress:"NY"}, {name:"John",age:"34",adress:"CF"}, {name:"Juddy",age:"42",adress:"LN"} ] return( <div> <h1>ユーザー</h1> <table className="user"> <thead> <tr><th>名前</th><th>年齢</th><th>住所1</th><th>住所2</th></tr> </thead> <tbody> <UserItem user={users[0]} /> <UserItem user={users[1]} /> <UserItem user={users[2]} /> </tbody> </table> </div> ) } const UserItem = (props) =>{ const { name,age,adress} = props.users return ( <tr> <td>{name}</td> <td>{age}</td> <td>{adress}</td> <td></td> </tr> ) } } UserItem.propTypes ={ users:PropTypes.object.isRequired } ReactDOM.render( <User />, document.getElementById('root') )
というコードがあります。
const UserItem = (props) =>{ const { name,age,adress} = props.users return ( <tr> <td>{name}</td> <td>{age}</td> <td>{adress}</td> <td></td> </tr> ) } }
の部分に対して質問です。
const { name,age,adress} = props.users
のprops.usersでなぜUserコンポーネントのusers配列を使えるのでしょうか?
スコープは関係ないのでしょうか?
また、
UserItem.propTypes ={ users:PropTypes.object.isRequired }
のusersはconst users の配列のことでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/02 14:09
2018/07/02 15:31
2018/07/08 02:18