รายละเอียดสำหรับเรียนรู้ React ปี 2020-2023 โดยโค้ชพล ดูหลักสูตรได้ที่ https://www.nextflow.in.th/react-training
เปิดไฟล์ src/components/MapBranch.js
ในที่นี้เราต้องการที่จะเรียกข้อมูลจาก Web API ตอนที่ MapBranch ถูกโหลดขึ้นมา ซึ่งเราจะ
เราจะ import getBranchLocation
จากไฟล์ actions มาใช้
import { getBranchLocation } from '../redux/actions';
mapDispatchToProps
ใน mapDispatchToProps
จะเป็น function ที่ Redux ส่ง function ที่ชื่อ dispatch เข้ามาให้เรา เราสามารถเชื่อม function ของเรากับ props ของ Component ในที่นี้
const mapDispatchToProps = (dispatch) => {
return {
// ประกาศ initData เป็น function ที่จะเรียกใช้
initData: () => getBranchLocation(dispatch),
}
}
componentDidMount()
เป็น function ที่จะทำงานหลังจาก Component แสดงขึ้นมาบนหน้าเว็บแล้ว จึงเหมาะมากที่เราจะเรียกใช้ function initData()
ของเรา
export class MapBranch extends Component {
componentDidMount() {
this.props.initData();
}