Reactjs 警告:无法对已卸载的组件执行反应状态更新…componentWillUnmount方法
我是一个非常新的反应和反应本机,我得到这个警告时,我切换屏幕。此外,console.log会无限重复,如何修复它Reactjs 警告:无法对已卸载的组件执行反应状态更新…componentWillUnmount方法,reactjs,react-native,axios,expo,Reactjs,React Native,Axios,Expo,我是一个非常新的反应和反应本机,我得到这个警告时,我切换屏幕。此外,console.log会无限重复,如何修复它 class DecodeScreen extends Component { state = { data: this.props.navigation.getParam("data", "NO-QR"), bookData: '', bookFound: false } bookSearch = () => { query = `
class DecodeScreen extends Component {
state = {
data: this.props.navigation.getParam("data", "NO-QR"),
bookData: '',
bookFound: false
}
bookSearch = () => {
query = `https://librarydb-19b20.firebaseio.com/books/${this.state.data}.json`,
axios.get(query)
.then((response) => {
const data = response.data ? response.data : false
console.log(data)
if (data) {
this.setState({
bookData: data,
bookFound: true
})
}
}).catch((error) => {
this.setState({
bookFound: false
})
})
}
renderContent = () => {
if (this.state.bookFound) {
return(
<View>
<TextH5>{this.state.bookData.title}</TextH5>
<TextH5>{this.state.bookData.author}</TextH5>
<TextH5>{this.state.bookData.publisher}</TextH5>
<TextH5>{this.state.bookData.isbn}</TextH5>
</View>
)
}
else {
return <TextH5>beer not found</TextH5>
}
}
componentDidMount() {
this.bookSearch()
}
render() {
{this.bookSearch()}
return (
<Container>
<TextH5>{this.state.data}</TextH5>
{this.renderContent()}
</Container>
);
}}
export default DecodeScreen;
您必须使用componentDidMount方法进行api调用
componentDidMount() {
this.bookSearch()
}
阅读react生命周期方法您可以尝试这种方法,看看它是否解决了问题 isMounted=错误; 类解码屏幕扩展组件{ 状态={ 数据:this.props.navigation.getParamdata,NO-QR, 账簿数据:, bookFound:错, }; bookSearch==>{ this.isMounted=true; 查询=`https://librarydb-19b20.firebaseio.com/books/${this.state.data}.json`, axios .getquery .thenresponse=>{ const data=response.data?response.data:false; 控制台日志数据; 如果数据{ 如果这是我的{ 这是我的国家{ bookData:数据, bookFound:没错, }; } } } .catcherror=>{ 这是我的国家{ bookFound:错, }; }; }; renderContent==>{ 如果找到此.state.bookFind{ 回来 {this.state.bookData.title} {this.state.bookData.author} {this.state.bookData.publisher} {this.state.bookData.isbn} ; }否则{ 未找到退货啤酒; } }; 组件安装{ this.isMounted=true; 这是一本书; } 组件将卸载{ this.isMounted=false; } 渲染{ { 这是一本书; } 回来 {this.state.data} {this.renderContent} ; } } 导出默认解码屏幕;
请在此处提供代码,而不是在picture@PedroMutter很抱歉,我现在编辑了它谢谢你的回复,但问题在更改屏幕后仍然存在,除了这个,我是否还遗漏了其他内容?