问题描述
伙计们,我正在尝试使用 react-native expo 从列表中删除一个元素
但它一直说 delete_post
不是函数
const ReduceBlogPost = (state,action) => {
switch (action.type) {
case 'delete_post':
return state.filter((blogs) => blogs.id !== action.playload);
case 'add_Blog_Post':
return [...state,{ id: Math.floor(Math.random() * 9999),title: `blog number one #${state.length + 1}` }];
default:
return state;
}
};
export const BlogProvider = ({ children }) => {
const [blogs,dispatch] = useReducer(ReduceBlogPost,[]);
// updateBlog
const updateBlog = () => {
dispatch({ type: 'add_Blog_Post' });
};
//delete blog post
const delete_blog = (dispatch) => {
return (id) => {
dispatch({ type: 'delete_post',playload: id });
};
};
return <BlogContext.Provider value={{ data: blogs,updateBlog }}>{children}</BlogContext.Provider>;
};
解决方法
为什么要在 var fragment; //assign default value to fragment
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
// you can add fragment based checks in this method
to work as you want
if (fragment is YourFragment) {
if (intent.getStringExtra("Type") != "START") {
//This would be for the first fragment
if (personId.text.toString().isEmpty()) {
val id = decodeScan((intent))
personId.text = id
lockId()
}
}
}else if (fragment is YourSecondFragment) {{
if (productId.hasFocus() || productId.text.isEmpty()) {
val res = decodeScan(intent)
productId.text = res
} else if (locationId.hasFocus() || (locationId.text.isEmpty() &&
productId.text.isNotEmpty())) {
val res = decodeScan(intent)
val location = (locations.filter { x -> x.contains(res) }).first()
locationId.setText(location)
}
}
}
}
函数中传递参数 dispatch
?您已经拥有从 delete_blog
取得的 dispatch
。
你可以喜欢这个
useReducer
我认为这取决于您的上下文。