January 14, 2021
μ λ² ν¬μ€ν μ λ°λλΌJS μμ store.getState() μ μν μ μννκ² νλ mapStateToProps ν¨μλ₯Ό ν΅ν΄,
Home Component μμ props λ‘ state λ₯Ό λ°μμ€κ² νλ κΈ°μ μ 보μλ€.
μ΄μ μ΄λ»κ² νλ©΄ component κ° dispatch λμλ ν μ μμμ§ μ΄ν΄λ³΄μ.
Home.js μ»΄ν¬λνΈμ μ λ² mapStateToProps λ₯Ό μ μΈνλ―μ΄ λ§λ€μ΄ μ£Όμλ€.
const mapDispatchToProps = (dispatch, ownProps) => {
console.log(dispatch)
}
μμ§ μμ ν κ°μ²΄λ₯Ό 리ν΄νκ² μ½λλ₯Ό λ€ μμ±νμ§λ μμμ§λ§ μΌλ¨ μ΄λλ‘ λκ³ ,
mapDispatchToProps μ΄ μμ΄λ λ§μ°¬κ°μ§λ‘ connect μ second argument μ λ€μ΄κ°λ€.
export default connect(mapStateToProps, mapDispatchToProps)(Home)
// export default connect(null, mapDispatchToProps)(Home);
// mapStateToProps κ° νμ μλ€λ©΄ μ΄λ κ² null λ‘ μ£Όλ κ²λ λλ€κ³ νλ€.
κ·Έλ κ² ν΄μ μ½μ.log λ‘ νμΈν΄λ³Έ dispatch μ λͺ¨μ΅μ μλμ κ°λ€.
Ζ dispatch(action) {
if (!isPlainObject(action)) {
throw new Error('Actions must be plain objects. ' + 'Use custom middleware for async actions.');
}
if (typeof action.type === 'undeβ¦
const mapDispatchToProps = (dispatch, ownProps) => {
return { dispatch }
}
μ΄μ Home Component μ props λ‘ λΆλ¬μμ dispatch λ₯Ό ν μ μκ² λλ€.
import { addToDo } from '../store'
μ‘μ ν¬λ¦¬μμ΄ν° ν¨μμΈ addToDo λ₯Ό λΆλ¬μμ,
handleSubmit ν¨μμ props μΌλ‘ λ°μμ¨ dispatch λ₯Ό μ€ννλ€.
const handleSubmit = event => {
event.preventDefault()
setText('')
props.dispatch(addToDo(text))
}
μ΄λ κ² props λ‘ λ°μμμ dispatch λ₯Ό λ 리면? μλμ΄ λλ€, κ·Έλ¬λ? μ΄λ κ² νλ κ² λμ μλ‘μ΄ props μ λ°μμ μ¬μ©ν΄λ³΄μ.
μλμ΄ μ λλ€.
Home.js μμ μ§μ μ μΌλ‘ dispatch λ₯Ό μ²λ¦¬νμ§ μκ² λ§λ€μ΄ 보μ. μΌλ¨ μ΄μ λ μ°¬μ°¬ν μκ°ν΄ λ³΄κ² λ€.
μ΄μ mapDispatchToProps λ‘ κ°μ μμ ν΄ λ³΄μ.
μμ export λ₯Ό ν΄μ νλ€. Why? μ΄μ λ μ΄μ Action Creator Function μ μ§μ μ¬μ© νμ§ μκ² νλ€.
const addToDo = text => {
return {
type: ADD,
text,
}
}
const deleteToDo = id => {
return {
type: DELETE,
id,
}
}
κ·Έλ¦¬κ³ μ½λ νλ¨μ μλμ κ°μ΄ export λ₯Ό νλ€.
export const actionCreators = {
addToDo,
deleteToDo,
}
μ΄μ Home.js μμ actionCreators λ₯Ό λΆλ¬ μ€κ² νλ©΄ λλ€.
// Home Component
import { actionCreators } from '../store'
import { actionCreators } from "../store";
..... μ€λ΅ .....
const mapDispatchToProps = (dispatch, ownProps) => {
return {
addToDo: (text) => dispatch(actionCreators.addToDo(text)),
};
};
addToDo λΌλ functionμ λ§λ€μλ€. μλ text argument λ₯Ό νμλ‘ νκ³ μ΄ ν¨μκ° μ€νλλ©΄μ dispatch λ₯Ό νΈμΆνλ€.
μ¦ addToDo ν¨μλ₯Ό props λ‘ λ³΄λ΄μ Home Component μμ props λ‘ addToDo λ₯Ό λ°μμμ μ¬μ©νκΈ° μ μ
console.log(props) λ‘ νμΈν΄ 보면?
{history: {β¦}, location: {β¦}, match: {β¦}, staticContext: undefined, toDos: Array(0), β¦}
addToDo: text => {β¦}
history: {length: 5, action: "POP", location: {β¦}, createHref: Ζ, push: Ζ, β¦}
location: {pathname: "/", search: "", hash: "", state: undefined}
match: {path: "/", url: "/", isExact: true, params: {β¦}}
staticContext: undefined
toDos: []
__proto__: Object
addToDo: text => {β¦} κ° ν¬ν¨λμ΄ μλ€!!!!!!!
const handleSubmit = event => {
event.preventDefault()
props.addToDo(text) // μ±κ³΅μ μΌλ‘ reducer μκ² dispatch νκ³ μλ€.
setText('')
}
μ΅μ’ μ μΌλ‘ μ΄μ λ΄ Home component λ μ§μ μ μΌλ‘ dispatch λ action creators λ₯Ό μ²λ¦¬ν νμκ° μκ² λλ€.
dispatch λ₯Ό home component μμ μ¬μ©νμ§ μλλ€.
ν¨μλ₯Ό λ§λ€μ΄μ props λ‘ μ λ¬νκ² νλ€λ κ²μ΄ μμ μΈ λ― νλ€.
νν 리λμ€ γ γ μ΄λ ΅λ€. νμ§λ§ κ³μ μλν΄ λ³΄λ λκ° λ νΈν κ±° κ°λ€λ λλ? μ μ¨λ€.
import React, { useState } from 'react'
import { connect } from 'react-redux'
import { actionCreators } from '../store' // ! μλ μν¬νΈ ν΄μμΌ νλ€.
// dispatch λ₯Ό home component μμ μ¬μ©νμ§ μλλ€. ν¨μλ₯Ό λ§λ€μ΄μ props λ‘ μ λ¬νκ² νλ€.
const Home = props => {
console.log(props)
const [text, setText] = useState('')
const handleChange = event => {
setText(event.target.value)
}
const handleSubmit = event => {
event.preventDefault()
props.addToDo(text) // μ±κ³΅μ μΌλ‘ reducer μκ² dispatch νκ³ μλ€.
setText('')
}
return (
<>
<h1>π€Something To Doπ€</h1>
<form onSubmit={handleSubmit}>
<input type="text" value={text} onChange={handleChange} />
<button>Add</button>
</form>
<ul>{JSON.stringify(props.toDos)}</ul>
</>
)
}
// ! store.getState() μ μν , props λ‘ state λ₯Ό λ°μμ€λ μ λ°ν¨μ΄λ..
const mapStateToProps = (state, ownProps) => {
return { toDos: state }
}
const mapDispatchToProps = (dispatch, ownProps) => {
return {
addToDo: text => dispatch(actionCreators.addToDo(text)),
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Home)