雖然<Image/>
組件不遵循重定向,但獲取並且可以用於獲取新的uri。
export const getRedirect = async (uri)=>{
const response = await fetch(uri)
const data = await response
return (data.status === 200 ? data.url : '')
}
這然後可以在一個新的組件可以使用這樣的:
class RedirectImage extends React.PureComponent {
constructor(props) {
super(props)
this.state = {uri: ''}
}
componentDidMount() {
getRedirect(this.props.uri).then((uri)=>{
this.setState({uri: uri})
})
}
render() {
return (
<Image
{...this.props}
source={{uri: this.state.uri}}
/>
)
}
}
RedirectImage.propTypes = {
uri: PropTypes.string.isRequired,
}
export default RedirectImage
現在,作爲期望<RedirectImage uri={'https://example.com/image.jpg'}/>
將顯示https://example.com/redirect.jpg
。