为什么 redux 状态可能未定义?
2022-01-13
149
在 redux 中我想创建一个选择器 getRequestStatus:
export const getRequestStatus = (state) => state.books.request
由于未知原因,当我使用
mapStateToProps
时它在组件中不起作用,并说
state.books
未定义。
const mapStateToProps = state => ({
getBook: (bookId) => getBook(state,bookId),
getRequestStatus: (state)=>getRequestStatus(state),//doesn't work, undefined state.books
})
令人惊讶的是,在组件中写入时它可以正常工作:
const request = useSelector((state)=>getRequestStatus(state))//works
当我不使用
useSelector
时,我尝试从 props 调用它:
const request = getRequestStatus()//doesn't work, cant access to undefined state.books
//normally it should because state is passed in mapStateToProps right?
我认为这是因为没有状态参数。你怎么看?
1个回答
getRequestStatus: (state)=>getRequestStatus(state)
应为
getRequestStatus: ()=>getRequestStatus(state)
dave
2022-01-13