React native react native can';t在componentDidMount上推送数据
React native react native can';t在componentDidMount上推送数据,react-native,React Native,“严格使用”; 从“react native navbar”导入导航栏; 从“反应本机可逆滚动视图”导入可逆滚动视图; 进口反应{ 评估学, 组成部分, 样式表, 文本, 看法 滚动视图, 异步存储, 触控高光, 可触摸不透明度, }从“反应本机”; var RNFS=require('react-native-fs'); var Thumb=React.createClass({ render:function(){ 返回( { }} > {this.props.node.name} {thi
“严格使用”;
从“react native navbar”导入导航栏;
从“反应本机可逆滚动视图”导入可逆滚动视图;
进口反应{
评估学,
组成部分,
样式表,
文本,
看法
滚动视图,
异步存储,
触控高光,
可触摸不透明度,
}从“反应本机”;
var RNFS=require('react-native-fs');
var Thumb=React.createClass({
render:function(){
返回(
{
}} >
{this.props.node.name}
{this.props.node.path}
);
}
});
var createThumbRow=(节点,i)=>;
导出默认类FileManager扩展React.Component{
建造师(道具){
超级(道具);
此.state={
isview:false,
文件列表:[],
};
};
组件willmount(){
让自我=这个;
readDir(RNFS.DocumentDirectoryPath)
。然后((结果)=>{
log('is find data',result);
返回Promise.all([RNFS.stat(结果[0].path),结果[0].path,结果[0].name]);
})
.然后((statResult)=>{
if(statResult[0].isFile()){
var tn=statResult[2];
var tp=statResult[1];
self.state.filelist.push({name:{tn},path:{tp});
self.setState({isview:true});
log('componentdidmountoffilelist in then',this.state.filelist);
}
返回“无文件”;
});
log('componentdidmountoffilelist out then',this.state.filelist);
}
makeItems(){
var项目=[];
var i=0;
for(此.state.filelist中的var c){
推(
);
}
退货项目;
}
render(){
var指数=0;
让自我=这个;
让titleConfig={title:'Meteor File',tintColor:'white'};
变量leftButtonConfig={
标题:"背",,
tintColor:“#fff”,
处理程序:函数onNext(){
self.props.navigator.push({
名称:“聊天机器人”
})
}
};
console.log('render of filelist',this.state.filelist);
返回(
{this.state.filelist.map(createThumbRow)}
);
}
};
const styles=StyleSheet.create({
容器:{
弹性:1,
},
项目左:{
背景颜色:“#e6f0ec”,
justifyContent:“flex start”,
对齐项目:“拉伸”,
差额:10,
身高:60,
},
});
在图片中,我已经找到了文件,并在之后将其推入“文件列表”,但“设置状态”不起作用,没有运行渲染功能。您可以从“文件列表的呈现”中看到。那么,怎么了?????救命啊 您不能直接将
.push()
推送到这个.state。更改状态的唯一正确方法是调用此.setState()。在您的情况下,您应该复制文件列表形式的状态,如let filelist=this.state.filelist
,然后推送对象filelist.push({name:{tn},path:{tp})
,然后使用this.setState({filelist:filelist})
将其设置为状态。您不能.push()
直接推送到此.state。更改状态的唯一正确方法是调用此.setState()。在你的情况下,你应该复制文件列表的状态,比如let filelist=this.state.filelist
,然后推一个对象filelist.push({name:{tn},path:{tp})
,然后用this.setState({filelist:filelist})
,请提供你的代码示例,而不是截图,而且几乎看不见。哦!对不起,我已经添加了。请提供您的代码样本,而不是截图,而且几乎看不见。哦!对不起,我已经加了。
'use strict';
import NavigationBar from 'react-native-navbar';
import InvertibleScrollView from 'react-native-invertible-scroll-view';
import React, {
AppRegistry,
Component,
StyleSheet,
Text,
View,
ScrollView,
AsyncStorage,
TouchableHighlight,
TouchableOpacity,
} from 'react-native';
var RNFS = require('react-native-fs');
var Thumb = React.createClass({
render: function() {
return (
<TouchableOpacity
style={styles.itemleft}
onPress={()=> {
}} >
<View style={{flexDirection:'column', backgroundColor:'#dbecf0'}} >
<View>
<Text
style={{fontSize: 30, textAlign: 'left',}}>
{this.props.node.name}
</Text>
</View>
<View>
<Text style={{fontSize:14, marginRight:5}}>{this.props.node.path}</Text>
</View>
</View>
</TouchableOpacity>
);
}
});
var createThumbRow = (node, i) => <Thumb key={i} node={node} />;
export default class FileManager extends React.Component {
constructor(props) {
super(props);
this.state = {
isview:false,
filelist: [],
};
};
componentWillMount() {
let self = this;
RNFS.readDir(RNFS.DocumentDirectoryPath)
.then((result) => {
console.log('is find data', result);
return Promise.all([RNFS.stat(result[0].path), result[0].path, result[0].name]);
})
.then((statResult) => {
if (statResult[0].isFile()) {
var tn = statResult[2];
var tp = statResult[1];
self.state.filelist.push({name:{tn}, path:{tp}});
self.setState({isview:true});
console.log('componentDidMount of filelist in then', this.state.filelist);
}
return 'no file';
});
console.log('componentDidMount of filelist out then', this.state.filelist);
}
makeItems() {
var items = [];
var i = 0;
for (var c in this.state.filelist) {
items.push(
<Thumb key={++i} name={c.name} path={c.path} />
);
}
return items;
}
render() {
var index = 0;
let self = this;
let titleConfig = { title: 'Meteor File', tintColor: 'white' };
var leftButtonConfig = {
title: 'Back',
tintColor: '#fff',
handler: function onNext() {
self.props.navigator.push({
name: 'ChatAndroid'
})
}
};
console.log('render of filelist', this.state.filelist);
return (
<View style={{flex: 1, backgroundColor:'#c2ede9'}}>
<NavigationBar title={titleConfig} leftButton={leftButtonConfig} tintColor="#1A263F"/>
<ScrollView style={{flex: 8}}>
{this.state.filelist.map(createThumbRow)}
</ScrollView>
</View>
);
}
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
itemleft: {
backgroundColor:'#e6f0ec',
justifyContent: 'flex-start',
alignItems: 'stretch',
margin: 10,
height:60,
},
});