๊ด€๋ฆฌ ๋ฉ”๋‰ด

yeon's ๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

[React-Native ๊ธฐ์ดˆ ๊ฐ•์˜] TouchEvent ๋ณธ๋ฌธ

Computer ๐Ÿ’ป/React

[React-Native ๊ธฐ์ดˆ ๊ฐ•์˜] TouchEvent

yeon42 2021. 7. 18. 17:06
728x90
React-Native TouchEvent ์ƒ์„ฑํ•˜๊ธฐ

 

 

 

- ์ƒˆ๋กœ์šด ํด๋”(src) ์•„๋ž˜ ์ƒˆ๋กœ์šด ํŒŒ์ผ(header.js) ์ƒ์„ฑ

import Header from './src/header'

...

<View style={styles.mainView}>
        <Header/>
      </View>

- import๋Š” ํ•„์ˆ˜ !

- View ์•ˆ์— importํ•œ Header๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

 

 


 

 

 

const ํ•จ์ˆ˜ = () => 
const Header = () => (
    <View style={styles.header}>
        <Text>This is header</Text>
    </View>
)

1. ์œ„์™€ ๊ฐ™์ด const ๋’ค์— ( )์—ฌ์•ผ์ง€ jsx๋ฅผ ๋ฆฌํ„ดํ•  ์ˆ˜ ์žˆ๋‹ค.

2. ๋งŒ์•ฝ const ๋’ค์— { }๋ผ๋ฉด ๋ฆฌํ„ด๋˜๋Š” js component๊ฐ€ ์—†๋‹ค๋Š” ๊ฒƒ!

 

  • jsx : javascript xml์˜ ์•ฝ์ž (javascript ํ™•์žฅ ๋ฌธ๋ฒ•)

 

 


 

class App extends Component {

  state = {
    appName: 'My First App'
  }


...

render() {
    return (
      <View style={styles.mainView}>
        <Header name={this.state.appName}/>
      </View>

- App.js์—์„œ state ์•ˆ์— ์žˆ๋Š” appName์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ header.js๋กœ name์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค.

 

const Header = (props) => (
    <View style={styles.header}>
        <Text>{props.name}</Text>
    </View>
)

- header.js์—์„œ๋Š” props๋กœ ์ธ์ˆ˜๋ฅผ ๋ฐ›์•„์˜ด!

 

 


 

 

View์—์„œ์˜ Touch ์ด๋ฒคํŠธ

TouchOpacity

• TouchableWithoutFeedback : ํˆฌ๋ช…ํ•ด์ง€์ง€ ์•Š๊ณ  alert์ฐฝ์ด ๋œธ

const Header = (props) => (
    <TouchableOpacity
        style={styles.header}
        onPress={()=>alert('hello world')}
    >
        <View>
            <Text>{props.name}</Text>
        </View>
    </TouchableOpacity>

 

 

 

- onLongPress : ์˜ค๋ž˜ ๋ˆŒ๋Ÿฌ์•ผ ์ด๋ฒคํŠธ ๋ฐœ์ƒ

onLongPress={()=>alert('hello world')}

 

- onPressIn : ๋ˆ„๋ฅด์ž๋งˆ์ž ๋ฐ˜์‘์ด ์˜ค๋„๋ก

onPressIn={()=>alert('hello world')}

 

- onPressOut : ํ„ฐ์น˜ํ•˜๊ณ  ์†์„ ๋†“์•˜์„ ๋•Œ ๋ฐ˜์‘์ด ์˜ค๋„๋ก

onPressOut={()=>alert('hello world')}
Comments