forked from wesley1001/RNApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mycreat.js
142 lines (127 loc) · 2.69 KB
/
mycreat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';//表示在严格模式下执行此js文件
var React = require('react-native');
var MoviesList = require('./mylist');
//定义变量
var {
StyleSheet,
Text,
View,
ScrollView,
TouchableOpacity,
TextInput
} = React;
//Api地址(json格式的)
var UserPost_URL = 'http://192.168.0.181:9000/api/users';
var MyCreat = React.createClass({
//获取初始的数据
getInitialState: function() {
return {
names: null,
ages: null,
citys: null
};
},
render: function() {
return (
<ScrollView style={{paddingTop:30}}>
<View>
<Text>姓名</Text>
<TextInput onChangeText={(text) => this.setState({names: text})}/>
</View>
<View>
<Text>年龄</Text>
<TextInput onChangeText={(text) => this.setState({ages: text})}/>
</View>
<View>
<Text>城市</Text>
<TextInput onChangeText={(text) => this.setState({citys: text})} />
</View>
<View>
<TouchableOpacity onPress={this.addUser}>
<View>
<Text>创建</Text>
</View>
</TouchableOpacity>
</View>
</ScrollView>
)
},
//post
addUser:function(){
var usersparm = {
name: this.state.names,
age: this.state.ages,
city: this.state.citys
};
fetch(UserPost_URL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(usersparm)
})
.then((response) => response.text())
.then((responseText) => {
//成功跳转页面
const { navigator } = this.props;
if(navigator) {
//把当前的页面pop掉,这里就返回到了上一个页面
this.props.navigator.push({name:"default",component:MoviesList});
};
})
.catch((error) => {
console.warn(error);
})
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',//垂直居中
alignItems: 'center',//水平居中
backgroundColor: '#F5FCFF',
marginTop:10
},
border:{
borderWidth: 1,
borderColor: '##CBCACA',
},
head: {
backgroundColor: '#9CAAF2',
},
thumbnail: {
width: 60,//默认以pt为单位
height: 80,
},
rightContainer: {
flex: 1,
},
newsTitle : {
color : '#4f4f4f',
fontSize : 18,
textAlign : 'center',
marginTop : 10,
marginBottom : 10,
fontWeight : 'bold'
},
title: {
fontSize: 20,
marginBottom: 8,
textAlign: 'center',
},
year: {
textAlign: 'center',
},
listView: {
paddingTop: 20,
paddingLeft:0,
backgroundColor: '#F5FCFF',
},
});
module.exports = MyCreat;