일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 파이썬 #알고리즘 #코딩테스트 #프로그래머스
- react #리액트 #동빈나 #나동빈 #유튜브강의
- java #자바 #동빈나
- 알고리즘
- 파이썬
- 파이썬 #백준 #알고리즘 #코딩테스트
- 프로그래머스 #파이썬 #코딩테스트 #알고리즘
- 자바 #java
- Dijkstra
- dp
- PYTHON
- 재귀
- BFS
- 백준 #알고리즘 #파이썬 #코딩테스트
- java #자바 #생활코딩
- 다익스트라
- css #웹 #생활코딩
- 프로그래머스 #파이썬 #알고리즘 #코딩테스트
- 백준 #파이썬 #알고리즘 #코딩테스트
- 프로그래머스
- 백준
- 투포인터
- 백트랙킹
- java #자바
- 코딩테스트
- DFS
- css #생활코딩 #웹
- java #자바 #나동빈
- 다이나믹프로그래밍
- react #리액트 #동빈나
Archives
- Today
- Total
커리까지
[동빈나] 12강 - 고객 추가 양식(Form) 구현 및 이벤트 핸들링 본문
728x90
SMALL
- npm install --save axios
- 서버와 통신하는 axios 설치하기
class CustomerAdd extends React.Component {
constructor(props) {
super(props);
this.state = {
file: null,
userName: '',
birthday: '',
gender: '',
job: '',
fileName: '',
open: false
}
}
}
- this.state 으로 초기화하기
render() {
return (
<form onSubmit={this.handleFormSubmit}>
<h1>고객 추가</h1>
프로필 이미지 : <input type="file" name="file" file={this.state.file} value={this.state.fileNmae} onChange={this.handleFileChange} /><br/>
이름 : <input type="text" name="username" value={this.state.userName} onChange={this.handleValueChange} /><br/>
생년월일 : <input type="text" name="birthday" value={this.state.birthday} onChange={this.handleValueChange}/><br/>
성별 : <input type="text" name="gender" value={this.state.gender} onChange={this.handleValueChanged}/><br/>
직업 : <input type="text" name="job" value={this.state.job} onChange={this.handleValueChanged}/><br/>
<buttom type="submit">추가하기</buttom>
</form>
)
}
}
export default CustomerAdd;
고객 정보를 form으로 받는다.
handleFileChange, handleValueChange, handleFormSubmit 3개 함수가 구현되야 함
addCustomer = () => { const url = '/api/customers'; const formData = new FormData(); formData.append('image', this.state.file); formData.append('name', this.state.userName); formData.append('birthday', this.state.birthday); formData.append('gender', this.state.gender); formData.append('job', this.state.job); const config = { headers: { 'content-type': 'multipart/form-data' } } return post(url, formData, config); }
특정 api주소로 form데이터를 보낸다.
이건 ajax에서도 새로운 form 데이터를 보낼때도 이렇게 사용함
post라이브러리는 axios에 포함되어 있음
handleFormSubmit = (e) => {
e.preventDefault()
this.addCustomer()
.then((response) => {
console.log(response.data);
this.props.stateRefresh();
})
this.setState({
file: null,
userName: '',
birthday: '',
gender: '',
job: '',
fileName: '',
open: false
})
}
- 이것도 ajax에서 form을 할 때 해당 form의 sumbit이 바로 실행되지 않도록 pre~를 해줌
handleFileChange = (e) => {
this.setState({
file: e.target.files[0],
fileName: e.target.value
})
}
- 실제로 setState에 있는 값을 변경해줌
- 왜 첫번째 값이냐?
- 우리는 파일을 1개만 업로드 할거니깐 그 파일중 0번째
handleValueChange = (e) => {
let nextState = {};
nextState[e.target.name] = e.target.value;
this.setState(nextState);
}
- State값을 변경해줌
import CustomerAdd from './components/CustomerAdd';
- app.js에 추가해줌
728x90
LIST
'react' 카테고리의 다른 글
[동빈나] 14강 - 부모 컴포넌트의 상태(State) 변경을 통한 고객 정보 갱신 (0) | 2021.07.04 |
---|---|
[동빈나] 13강 - Node.js Express에서 파일 업로드 요청 처리 및 DB에 데이터 삽입 (0) | 2021.07.03 |
concurrently --kill-others-on-fail "yarn sever" "yarn client" 'concurrently'은(는) 내부 또는 외부 명령, 실행할 수 있는 프로그램, 또는 배치 파일이 아닙니다. (0) | 2021.06.27 |
11강 - 고객(Customer) DB 테이블 구축 및 Express와 연동하기 (0) | 2021.05.28 |
[동빈나] 10강 - AWS RDS 서비스를 이용하여 MySQL DB 구축하기 (0) | 2021.05.24 |
Comments