[英]how to setup useRef current.value inside your react native component without render?
my issue is my detail screen will keep loading in order to getAPI back everytime i try to add quantity using State its really annoying.我的问题是,每次我尝试使用 State 添加数量时,我的详细信息屏幕将继续加载以获取 API,这真的很烦人。 so far my best option is only using useRef to pre-caution my screen from rendering.
到目前为止,我最好的选择是只使用 useRef 来预防我的屏幕渲染。 is there any advise or best practice to solve this issue?
是否有解决此问题的建议或最佳做法? my goal is only to display my current.value from useref please help.
我的目标只是从 useref 显示我的 current.value 请帮助。 thank you.
谢谢你。
here is my code:这是我的代码:
export default function ProductDetail({route, navigation}) {
const id = route.params.itemId;
const {addToCarts, carts} = useStore();
const refQty = useRef(1);
const handlerIncreaseQty = () => {
refQty.current = refQty.current + 1;
console.log(refQty.current);
};
const handlerDecreaseQty = () => {
if (refQty.current <= 1) {
return;
}
refQty.current = refQty.current - 1;
console.log(refQty.current);
};
here is my render:这是我的渲染:
<View style={tw`flex-1 border p-4 my-2 rounded-xl bg-yellow-300 `}>
<View style={tw`flex-1`}>
<Image
style={{
width: '100%',
height: '100%',
resizeMode: 'center',
}}
source={{uri: `${data.image}`}}
/>
</View>
<View style={tw`flex-1 my-1`}>
<Text style={tw`bg-green-400 p-2`}>{data.description}</Text>
</View>
<View style={tw`flex-row justify-evenly`}>
<View style={tw`flex-row items-center`}>
<Button title="-" onPress={handlerDecreaseQty} />
<Text style={tw`mx-3`}>{refQty.current}</Text>
<Button title="+" onPress={handlerIncreaseQty} />
</View>
<View>
<Button
title="add to cart"
onPress={() => handlerAddToCarts(data)}
/>
</View>
<Button
title="Go back"
onPress={() => {
navigation.goBack();
}}
/>
</View>
</View>
my screen looks like:我的屏幕看起来像:
This is not possible.这是不可能的。 You are trying to display a the current value of a
ref
while dynamically changing it in your UI.您正在尝试显示
ref
的当前值,同时在您的 UI 中动态更改它。 This is what states
are for by design of the framework.这就是框架设计的
states
。
A screen will only update its information visually, this means it rerenders in a new render cycle, if its current state somehow changes.屏幕只会在视觉上更新其信息,这意味着如果其当前的 state 发生某种变化,它将在新的渲染周期中重新渲染。
This is by design.这是设计使然。 If you do not want this to happen, then you might want to consider a different framework.
如果您不希望这种情况发生,那么您可能需要考虑一个不同的框架。
If you have an API call which is triggered everytime your screen rerenders, then you have a different problem.如果每次屏幕重新呈现时都会触发 API 调用,那么您会遇到不同的问题。 There is a standard pattern to solve this.
有一个标准模式可以解决这个问题。
React.useEffect(() => {
// fetch data and set state
// the dependency array is empty, thus this will only be called once
}, [])
If you use a hook that handles your API calls, then you might want to consider adding an application cache
such as Vercel's SWR .如果您使用处理 API 调用的挂钩,那么您可能需要考虑添加
application cache
,例如Vercel 的 SWR 。
const DetailsScreen = () => {
const [quantity, setQuantity] = useState(1);
const [product, setProduct] = useState();
useEffect(() => {
const data = // get your data;
setProduct(data);
}, []);
};
This is how you should structure your component.这就是您应该构建组件的方式。 If this is not what you need make a comment.
如果这不是您需要的,请发表评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.