Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 用状态错误响应本机更改img_Reactjs_React Native - Fatal编程技术网

Reactjs 用状态错误响应本机更改img

Reactjs 用状态错误响应本机更改img,reactjs,react-native,Reactjs,React Native,首先,我很抱歉,因为我的英语水平很低 我在学习英语。 尝试使用状态更改图像,但不工作。 错误:null不是对象(this.state.admob) index.js class HomeScreen extends React.Component { constructor(props){ super(props); this.state = {admob: require('../Img/admob1.jpg'), admobCount: 0}

首先,我很抱歉,因为我的英语水平很低

我在学习英语。 尝试使用状态更改图像,但不工作。 错误:null不是对象(this.state.admob)

index.js


   class HomeScreen extends React.Component {    
   constructor(props){
       super(props);
       this.state = {admob: require('../Img/admob1.jpg'), admobCount: 0};
   }

   _changeAdmob(){
       this.setState({
           if(admobCount = 0){
               admobCount: this.state.admobCount + 1;
               admob: require('../Img/admob1.jpg');
           },
           if(admobCount = 1){
               admobCount: 0;
               admob: require('../Img/admob2.png');
           }
       });
   }

   componentDidMount(){
       this.admobTimer = setInterval(
           () => this._changeAdmob(), 2000
       );
   }

   componentWillUnmount(){
       clearInterval(this.admobTimer);
   }

   render() {                          
     return (          
         <View style={styles.MasterContainer}>
             <NavBar navigation = {this.props.navigation}/>
             <UserBar navigation = {this.props.navigation}/>
             <View style={{height: 40,}}></View>
             <ButtonTab/>
             <Admob/>
             <TapBar/>
         </View>
     );
   }
  }

   class Admob extends React.Component{
     render() {
         return(
             <View style={{ flex: 1,alignItems: 'center',justifyContent:'center' }}>
                 <Image style={{ width: 350, height: 70 }} source={this.state.admob}></Image>                 
             </View>
         );
     }
  }

类主屏幕扩展React.Component{
建造师(道具){
超级(道具);
this.state={admob:require('../Img/admob1.jpg'),admobCount:0};
}
_changeAdmob(){
这是我的国家({
如果(admobCount=0){
admobCount:this.state.admobCount+1;
admob:require('../Img/admob1.jpg');
},
如果(admobCount=1){
admobCount:0;
admob:require('../Img/admob2.png');
}
});
}
componentDidMount(){
this.admobTimer=setInterval(
()=>这个._changeAdmob(),2000
);
}
组件将卸载(){
clearInterval(this.admobTimer);
}
render(){
报税表(
);
}
}
类Admob扩展了React.Component{
render(){
返回(
);
}
}

**我想在2秒钟内更改映像。**

您需要在组件上传递状态

将此添加到
index.js

<View style={styles.MasterContainer}>
          <NavBar navigation = {this.props.navigation}/>
          <UserBar navigation = {this.props.navigation}/>
          <View style={{height: 40,}}></View>
          <ButtonTab/>
          <Admob admob={this.state.admob}/>
          <TapBar/>
</View>

Admob.js将this.state更改为this.props



如何将url字符串直接保存到
this.state.admob
?我没有检查所有的代码,但快速浏览,您使用“=”进行比较,而您应该使用“==”,使用“:”设置变量值,而您应该使用“=”。改变这个,看看会发生什么。[例如,与if(admobCount=1)admobCount:0不同,您应该编写if(admobCount==1)admobCount=0;]下面的答案当然不那么重要……感谢您的回答。好好干!但是我的changeAdmob方法不起作用。。我正在修复它..在函数_changeAdmob()上添加此项。对于此_changeAdmob=()=>{….}
<Image style={{ width: 350, height: 70 }} source={this.props.admob}></Image>