我刚开始学习 ReactJS。现在我想知道当我使用 fetch 发出 API 请求时如何处理响应状态。这是我的代码:
componentDidMount(){
this.setState({ isLoading: true })
var id = this.props.match.params.id
const api = `bla/bla/${id}`;
console.log("start call api")
fetch(api)
.then((response) => {
if(response.status === 200){
console.log("SUCCESSS")
return response.json();
}else if(response.status === 408){
console.log("SOMETHING WENT WRONG")
this.setState({ requestFailed: true })
}
})
.then((data) => {
this.setState({ isLoading: false, downlines: data.response })
console.log("DATA STORED")
})
.catch((error) => {
this.setState({ requestFailed: true })
})
console.log("end call api")
}
Run Code Online (Sandbox Code Playgroud)
我关闭了我的连接以进行测试408,但我的加载仍然出现。
render(){
const { isLoading, requestFailed } = this.state; …Run Code Online (Sandbox Code Playgroud) 我尝试像这样设置边框半径:
const styles = {
btn:{
height: 30,
fontSize: 11,
borderRadius: 50
}
}
Run Code Online (Sandbox Code Playgroud)
它不起作用。我已阅读文档但找不到答案。
我想让灰色框的位置固定,所以当我scrolldown或scrollup灰色框保持静止在其位置上时。
在 React Native 中可以做这样的事情吗?
这是我的代码:
<View>
<ScrollView>
<View style={styles.continueToPaymentContainer}>
</View>
</ScrollView>
</View>
container: {
flex: 1,
// justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff'
},
continueToPaymentContainer:{
width:width,
height: 100,
position:'absolute',
bottom:0,
backgroundColor: 'grey',
}
Run Code Online (Sandbox Code Playgroud)
我是使用 React Native 的新手。
我只是按照屏幕截图创建了一个应用程序。
我想让dropdownorPicker组件小一点。
但是我做不到,我正在寻找解决此问题的指南。
谢谢。
import React, { Component } from 'react'
import { Item, Picker, TouchableOpacity, View, ListView, Text } from 'react-native'
import { connect } from 'react-redux'
import { Icon, Header } from 'react-native-elements'
import { Button, Container, Content, Footer, Title} from 'native-base'
// For empty lists
// import AlertMessage from '../Components/AlertMessage'
// Styles
import styles from './Styles/MainRestoStyles'
class MainRestoScreen extends React.Component {
constructor (props) {
super(props)
/* ***********************************************************
* …Run Code Online (Sandbox Code Playgroud)我想从这个React Router获取id值:
<BrowserRouter basename="/api">
<Switch>
<Route path="/pm" exact component={PaymentMethod}/>
<Route path="/pw" exact component={PaymentWeb}/>
<Route path="/rg" exact component={Registrasi}/>
<Route path="/bonus/:id" exact component={BonusScreen}/>
<Route path="/hb" exact component={HistoryBonus}/>
</Switch>
</BrowserRouter>
Run Code Online (Sandbox Code Playgroud)
在我BonusScreen我试图通过使用以下命令打印值:
const userId = this.props.match.id;
console.log(this.userId);
Run Code Online (Sandbox Code Playgroud)
从浏览器,我尝试像这样访问URL:
bonus/NGO628567652201
Run Code Online (Sandbox Code Playgroud)
但是它总是回来了undefined。
我怎样才能解决这个问题 ?
如何自定义 React Navigation 的 headerLeft TabNavigator。
这是我的屏幕之一:
Back我想从 中删除headerLeft
可以吗?
这是我的代码:
DetailTabs = TabNavigator({
DetailResult:{
screen:DetailResult,
navigationOptions:{
title:'Detail Penginapan',
headerTitleStyle:{
fontSize:14,
textAlign: "center",
flex: 1,
},
tabBarVisible: false,
headerStyle:{
backgroundColor:'#4A94FB',
borderBottomColor: 'transparent',
},
headerTintColor: 'white'
}
}
})
Run Code Online (Sandbox Code Playgroud) 我绝对是 React Native、Redux 和朋友的初学者。
现在我尝试使用 React Native 和 Redux 创建一个应用程序。
在这个过程中我发现了这个错误:
我尝试在我的应用程序中使用 Redux,如下所示:
export const confirmRegister = (userdata) => {
return (dispatch) => {
return(
fetch(URI + path, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'x-application-id' : AppId,
'hashing' : this.generateHashing(userdata)
},
body: userdata,
})
.then((response) => {
return response.json()
})
.then((response)=>{
dispatch(setDataAfterRegister(response.result))
})
.catch(error => {alert(error)})
)
};
};
export function setDataAfterRegister(data){
return{
type:'REGISTERED',
name:data.name,
token:data.access_token
}
}Run Code Online (Sandbox Code Playgroud)
我还在学习如何使用 Redux。
任何帮助将不胜感激。
这是我的商店:
import { createStore } from …Run Code Online (Sandbox Code Playgroud)reactjs ×5
react-native ×4
javascript ×3
android ×1
api ×1
css ×1
fetch-api ×1
material-ui ×1
redux ×1
url ×1