0
1

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 1 year has passed since last update.

React props簡単な使い方

Posted at

そもそもpropsは親から子に要素を渡すヤツ(正直賢い人が書いたの見てもよくわからんから未来の自分がわかるように書く)

親.jsx
     <View>
      <Item />
    </View>
子.jsx
const Item = () => {
  return (
      <View>
        <Image source={{ uri:'httpなんたらかんたら.com' }} />
      </View>
      <View >
        <Text numberOfLines={3}>
          なんたらかんたら
        </Text>
        <Text style={styles.subText}>なんたらかんたら作者</Text>
      </View>
  );
};

上記の奴はまだPropsとか使ってない状態。
これでもちゃんと動くし普通に見れる。

Propsを使って書いてみる

親.jsx
     <View>
      <Item author="なんたらかんたら" imageUrl="httpなんたらかんたら.com" content="なんたらかんたら"/>
    </View>
子.jsx
const Item = (props) => {
  return (
      <View>
        <Image source={{ uri: props.imageUrl }} />
      </View>
      <View >
        <Text numberOfLines={3}>
          {props.content}
        </Text>
        <Text style={styles.subText}>{props.author}</Text>
      </View>
  );
};

これで全く同じPropsを使って全く同じ見た目のものが作れた!

ちなみに子.jsxのPropsを変えてあげると

子.jsx
const Item = ({ imageUrl, content, author }) => {
  return (
      <View>
        <Image source={{ uri: imageUrl }} />
      </View>
      <View >
        <Text numberOfLines={3}>
          {content}
        </Text>
        <Text style={styles.subText}>{author}</Text>
      </View>
  );
};

いちいちPropsって書かなくてもいいようになる。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?