你好,我在应用程序中多次使用选择器。 我可以编写一次代码然后导入选择器吗? 我曾想编写一个自定义钩子,但我认为这是没有必要的。 这是我的选择器:
...
# EVENTS
for event in pygame.event.get():
# Quitting
if event.type == pygame.QUIT:
sys.exit()
pressed = pygame.key.get_pressed()
jet_xincrement = 0
if pressed[pygame.K_RIGHT]: jet_xincrement += 3
if pressed[pygame.K_LEFT]: jet_xincrement -= 3
boundaries()
move_jet(jet_xincrement)
pygame.display.flip()
如果我有这样的东西,那就太好了
const county = useSelector((state)=>{
const id = state.user.id
const name = state.user.name
return state.users.filter((user) => user.name === name)[0].county
})
答案 0 :(得分:4)
您可以在useSelector
中传递箭头功能
export const countySelector = state => {
const id = state.user.id;
const name = state.user.name;
return state.users.find(user => user.name === name).county;
};
// Usage
import { countySelector } from 'selectors';
const county = useSelector(countySelector);
或创建自定义钩子
const useCounty = () => {
return useSelector(state => {
const id = state.user.id;
const name = state.user.name;
return state.users.find(user => user.name === name).county;
});
};
export default useCounty;
// Usage
import useCounty from 'useCounty';
const county = useCounty();
答案 1 :(得分:2)