为什么 redux 状态可能未定义?
Why redux state might be undefined?
在 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
时,我尝试从道具中调用它:
const request = getRequestStatus()//doesn't work, cant access to undefined state.books
//normally it should because state is passed in mapStateToProps right?
我想这是因为没有状态参数。你怎么看?
getRequestStatus: (state)=>getRequestStatus(state)
应该只是 getRequestStatus: ()=>getRequestStatus(state)
在 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
时,我尝试从道具中调用它:
const request = getRequestStatus()//doesn't work, cant access to undefined state.books
//normally it should because state is passed in mapStateToProps right?
我想这是因为没有状态参数。你怎么看?
getRequestStatus: (state)=>getRequestStatus(state)
应该只是 getRequestStatus: ()=>getRequestStatus(state)