Open bakyuns opened 5 years ago
terminal 상에서
expo start
#혹은
yarn start
chrome으로 들어가지게 되면,
IOS Simulator 시작하고 Expo 앱을 통하여 현재 나의 앱 확인
watchman을 통하여 나의 소스가 변경되어 저장되면 바로 반영이 된다.
현재 사용하고 있는 Typescript는 Javascript의 슈퍼셋으로, Javascript의 Paramater가 무조건적으로 Any인 것에 반하여 Type에 대하여 정의를 해줄 수 있는 것이 특징이다.
이는 Bable 등을 통하여 JS로 다시 변환되어 실질적 결과물은 JS이다.
현재 Typescript를 사용 한 부분을 보면 https://github.com/poemyear/pickpic/blob/7ede60dfc90f4c914e390a636b6ea1973c42c30a/pickpicApp/App.tsx#L9-L18
위와 같이 interface로 Props와 States를 지정해주고, https://github.com/poemyear/pickpic/blob/7ede60dfc90f4c914e390a636b6ea1973c42c30a/pickpicApp/App.tsx#L20-L24 위와 같이 React.Component에 대하여 prop과 state의 type을 한정시키며, 생성자에서 또한 마찬가지로 Props 형태로 한정시킨다. 이를 통하여 개발자는 다른 곳에서 state나 props를 사용 할 때, 적절한 타입을 사용하지 않으면 컴파일에러가 발생하여 런타임 시 발생할 에러에 대하여 미연에 방지한다.
export default
Event 내에서 함수의 호출
아래의 render는 upload 부이다. https://github.com/poemyear/pickpic/blob/7ede60dfc90f4c914e390a636b6ea1973c42c30a/pickpicApp/src/Scene/Upload/index.tsx#L46-L63
View안에 Button을 만들고 button을 눌렀을 시, onPress 이벤트를 발발하여 이에 대한 함수를 실행시킨다.
여기서는 _pickImage라는 함수를 실행시켰으며, 이는 ImagePicker 이고, 이 함수내에서 setState를 통하여 state에 저장한다.
그 후, Send to Node.js라는 Button을 누를 시 sendImage함수를 호출하여 이를 서버에게 전송한다.
Server에게 전송하는 부분은 아래와 같다. https://github.com/poemyear/pickpic/blob/7ede60dfc90f4c914e390a636b6ea1973c42c30a/pickpicApp/src/Scene/Upload/index.tsx#L14-L44
이미지 데이터는 userfile에 쓸 것이며, 이는 TImage라고 정의한 type에 맞춰서 넘겨줘야 한다.
이를 위하여, uploadImage를 이 형식에 맞춰 채워주고, formdata에 이 값을 넣어준다.
여기서 문제가 발생하였었는데, formdata에서 append
에 대하여 any가 아닌 string|blob으로 설정해놔서 애를 먹음
fetch를 통하여 서버에게 전송하며 콜백으로 실패와 성공여부를 알 수 있다.
https://reactjs.org/docs/faq-functions.html#how-do-i-bind-a-function-to-a-component-instance 읽어보면 좋음
@bakyuns Event 내에서 함수 호출 시 전자 후자 모두 호출시마다 새로운 함수를 생성하는 방법이라고 함, Ref
Using Function.prototype.bind in render creates a new function each time the component renders, which may have performance implications (see below).
Using an arrow function in render creates a new function each time the component renders, which may break optimizations based on strict identity comparison.
권장안이 있는건 아닌듯; Bind in Constructor or Class Properties 인데 Class Properties는 아직 실험단계라고 하고, Bind in Constructor도 깔끔한거같진않음
https://blog.jaeyoon.io/2018/01/react-bind.html
https://blueshw.github.io/2017/07/01/arrow-function/
// 생성자에서 bind()를 사용하는방법
class LoggingButton extends React.Component {
Constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
console.log('this is:', this);
}
render() {
return (
<button onClick={this.handleClick}>
Click me
</button>
);
}
}
// 사용할 곳에서 bind()를 사용하는 방법
class LoggingButton extends React.Component {
handleClick() {
console.log('this is:', this);
}
render() {
return (
<button onClick={this.handleClick.bind(this)}>
Click me
</button>
);
}
}
// 화살표 함수로 직접 호출하는 방법
// 이 방법은 LoggingButton이 랜더링 될 때마다 새로운 함수를 생성하는 문제가 있다.
// 콜백 함수 내에서 재랜더링을 발생시키는 경우 성능 문제가 발생할 수 있다.
class LoggingButton extends React.Component {
handleClick() {
console.log('this is:', this);
}
render() {
return (
<button onClick={(e) => this.handleClick(e)}>
Click me
</button>
);
}
}
// 화살표 함수로 선언하고 바인딩하는 방법
// 이 방법으로 이벤트를 바인딩 하는것을 권장한다.
class LoggingButton extends React.Component {
handleClick = () => {
console.log('this is:', this);
}
render() {
return (
<button onClick={this.handleClick}>
Click me
</button>
);
}
}
yarn add react-native-navigation yarn add react-native-gesture-handler
Install
Platform
React Native
Expo CLI Download
하지만, 현재 pickpic repo에서는 이에 대해서 expo init을 해놨으므로 따로 init은 필요 없다.
React Native 패키지 관리
npm이 아닌 yarn으로 사용하는 이유는 yarn에서는 -D 옵션을 통하여 devdependency에 대한 packages를 받을 수 있다. devdependency는 대표적으로 **babel**이 존재하며, 이는 자바스크립트 컴파일러다. 내가 알기로는 typescript 혹은 flow에 대하여 컴파일시 js로 변환해주는 작업을 해주는 것으로 알고 있다.
Expo 패키지 관리
현재 사용되고 있는 Packages
현재 pickpic에서 사용하고 있는 Packages
ref