简体   繁体   English

如何在没有渲染的情况下在你的本机组件中设置 useRef current.value?

[英]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.

相关问题 React Native如何获取useRef current的值 - How to get the value of useRef current in React Native React:如何从 useEffect 内部返回 useRef.current.value? - React: How to return useRef.current.value from inside useEffect? 如何在React Native中呈现文本组件内部的组件数组? - how to render array of component inside Text component in react native? 如何读取子组件中的当前 React.useRef? - How can I read the current React.useRef in the child component? 如何在React Native中基于AsyncStorage值渲染组件? - How do you render a component based on an AsyncStorage value in React Native? React Native - useRef 在 RNCamera 上给出空值 - React Native - useRef is giving null value on RNCamera 如何在React Native中组件的render函数内部的对象内调用类? - How to call a class inside an object that's inside the render function of a component in React Native? 如何在本机反应中有条件地呈现警报组件 - How to render Alert Component conditionally in react native 如何在 React Native 中只渲染一次组件 - How to render component only once in React Native 无法在 React Native 中使用 useRef() 将 current.focus() 应用于按钮 - Cannot apply current.focus() to button using useRef() in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM