我正在尝试从API调用后的状态动态加载表单,此操作是在int main()
{
/*int i, arr[][], j, m, n;*/
/* Because arr is allocated dynamically, you have to declare as a pointer to int. */
int i, *arr, j, m, n;
int max = 0;
int *ptr;
printf("enter the value m");
scanf("%d", m);
printf("enter the vaue of n");
scanf("%d", n);
/*ptr = (int*)malloc(m * n * 4);*/
/* It is better to use sizeof(int) because int does not have the same length of all computers. */
ptr = (int*)malloc(m * n * sizeof(int));
printf("enter the values\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", ((ptr + i * n) + j));
}
}
/*max = arr[0];*/
/* To get the first int at arr, you could also use arr[0], but not arr[0][0] because */
/* this would require that arr be an array of pointers to array of int's, which is not the case. */
max = *arr;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (max < *((ptr + i * n) + j));
max = *((ptr + i * n) + j);
}
}
printf("%d", max);
}
中完成的。 API调用成功,完成后在componentDidMount()
上调用setState()
,但是languageList
调用不返回任何选项。它什么也不返回。
.map()
我已验证 componentDidMount() {
let languages = []
base('Languages').select({
view: 'Grid view'
}).firstPage(function(err, records) {
if (err) {console.error(err); return; }
records.forEach(function(record) {
let languageObject = {}
languageObject['id'] = record.id
languageObject['name'] = record.get('Language Name')
languages.push(languageObject)
})
})
this.setState({
languageList: languages,
})
}
render() {
return (
...
<select>
{this.state.languageList.map((language, key) => (
<option key={key} value={language.id}>{language.name}</option>
))}
</select>
)
}
是对象数组,每个对象都有一个state.languageList
和id
。
我想念什么?
答案 0 :(得分:1)
将setState
移到API回调中。...当数据可用时
您正在调用它,因为它是异步的
base('Languages').select({
view: 'Grid view'
}).firstPage((err, records) => {// arrow function to not block `this`
if (err) {
console.error(err);
return;
}
records.forEach(function(record) {
let languageObject = {}
languageObject['id'] = record.id
languageObject['name'] = record.get('Language Name')
languages.push(languageObject)
})
// languages array has been updated here
this.setState({
languageList: languages,
})
})
答案 1 :(得分:0)
尝试一下
<select>
{
this.state.languageList && this.state.languageList.map((language, key) => (
<option key={key} value={language.id}>{language.name}</option>
))
}
</select>