简体   繁体   English

在 React 中安装组件时如何刷新照片链接引用?

[英]How to refresh a photo link reference when a component mounts in React?

I have a component like so:我有一个像这样的组件:

export function SomeComponent(props) {
  const { id } = props;

  const photo = `http://someurl.com/photo/${id}/`;

  return (
    <View>
      <Avatar
        rounded
        source={photo}
      />
    </View>
  );
}

I have a photo update endpoint which updates the photo that is stored in http://someurl.com/photo/${id}/ .我有一个照片更新端点,它更新存储在http://someurl.com/photo/${id}/的照片。

The issue is after the update and I visit the route where this component is shown, it's still showing the old photo.问题是在更新之后,我访问了显示此组件的路线,它仍然显示旧照片。 Only if I refresh the page, will the photo get updated.只有当我刷新页面时,照片才会更新。

Is there a way to force reload the content of the link?有没有办法强制重新加载链接的内容? Or do I need to force reload the route that contains the component?或者我是否需要强制重新加载包含组件的路由?

the page re-renders when the states or props are updated, you can manually trigger re-render by setting up a state or prop and changing its value.当状态或道具更新时页面重新渲染,您可以通过设置状态或道具并更改其值来手动触发重新渲染。

one scenario:一种情况:

ParentComponent() {
  const [trigger, setTrigger] = useState(false);

  const onUpdate = () => {
   setTrigger(false);  
 }
return (
 ..... your code 
 <SomeComponent trigger={trigger} />  // trigger is passed as prop so as its value changes component re-renders
  ) 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM