我有一个react应用程序的js文件,它使用mapStateToProps,并包含以下两个函数:
const mapStateToProps = (state) => {
return {
users: state.gf.users,
employees: state.gf.employees,
}
}
const EmployeeListFilter = () => {
const getEmployees = () => {
let employees = this.props.employees
}
useEffect(() => {
if (empId) {
getEmployees()
}
}, [empId])
}
函数mapStateToProps起作用,我可以放一个断点,看看这是如何处理员工列表的。
然后如何从getEmployees中访问该员工列表?this.props.employees说"这"是未定义的。
解决方案是在中传递道具
const mapStateToProps = (state) => {
return {
users: state.gf.users,
employees: state.gf.employees,
}
}
const EmployeeListFilter = (props) => {
const getEmployees = () => {
let employees = props.employees
}
useEffect(() => {
if (empId) {
getEmployees()
}
}, [empId])
}