См. https://golang.org/src/net/http/client.go «Когда err равно nil, resp всегда содержит non-nil resp.Body."
, но они не говорят, когда err! = nil, resp всегда равно нулю. Далее они говорят: «Если resp.Body не закрыт, базовый RoundTripper (обычно Transport) Клиента, возможно, не сможет повторно использовать постоянное TCP-соединение с сервером для последующего запроса« keep-alive »».
Поэтому я, как правило, решил проблему следующим образом:
client := http.DefaultClient
resp, err := client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
Вы можете визуализировать обычный View или ScrollView на основе какого-либо условия, рассмотрите этот пример:
class MyComponent extends React.Component {
constructor(props){
super(props)
this.state = {
childHeight: 0,
screenHeight: Dimensions.get('window').height
}
}
render(){
const wrapper = this.state.childHeight > this.state.screenHeight ? ScrollView : View
return (
<Wrapper>
//onLayout passes a an object with a synthetic event that has a nativeEvent prop
//I pull off the nativeEvent prop using deconstruction so I can get the height
<View onLayout={
({ nativeEvent }) => {
this.setState({ childHeight: nativeEvent.layout.height })
}}>
{children}
</View>
</Wrapper>
)
}
}
Это всего лишь одна реализация, вы можете сделать это так, как вам нравится. Вот еще немного информации о onLayout prop.
Если вы не можете или не хотите использовать вид, вам придется выполнить какой-либо другой вид расчета макета. Одним из способов является манипулирование вашими стилями и опорой детей:
const myStyle = StyleSheet.create({
childStyle: {
height: 180
}
})
class MyComponent extends React.Component {
constructor(props){
super(props)
this.state = {
childHeight: React.Children.count(props.children) * 180,
//get the count of how many children have been passed to your component
//and multiple it by whatever height your children have set in their styles
screenHeight: Dimensions.get('window').height
}
render(){
const wrapper = this.state.childHeight > this.state.screenHeight ? ScrollView : View
return (
<Wrapper>
{children}
</Wrapper>
)
}
}
Если число детей может меняться в течение нескольких визуализаций, я бы предложил узнать о компоненте FlatList