reactjs - 当我在子组件中提交表单时,如何更新我定义了 contexAPI 的父类的状态?
问题描述
当我从子组件提交表单时,如何更新定义父上下文 api 的类的状态?
import React, {createContext,Component} from 'react'
import http from './services/httpService'
import {MyApiUrl} from './services/httpService'
export const GlobalState = createContext()
export class DataProvider extends Component{
state={
jobs:[],
loadstate:false,
}
async populateCategories() {
const {data:jobs}=await http.get(MyApiUrl+'/api/jobs')
this.setState({jobs})
console.log('hellow world')
return
}
async componentDidMount(){
await this.populateCategories()
}
logPokemon = this.populateCategories.bind(this);
// async setUser() {
// await this.populateCategories()
// }
render(){
const {jobs}=this.state;
const{logPokemon}=this
const{children}=this.props
return (
<GlobalState.Provider value={{logPokemon,jobs}}>
{children}
</GlobalState.Provider>
)
}
}
现在来自另一个子组件,当提交表单时,我这样做了,我收到警告:无法对未安装的组件执行 React 状态更新
static contextType = GlobalState
const {logPokemon} = this.context;
logPokemon()
解决方案
推荐阅读
- html - 如何在 React html 解析器中使用事件
- python - 蝗虫中每个线程的不同数据集/蝗虫中的线程控制
- git - VS Code 发布到 GitHub,但存储库为空
- javascript - Javascript 静态方法链接
- c# - 为什么 svchost.exe 会终止我的进程
- python - Pandas Row 用 NaN 表示
- ssh - 在通过 ssh 连接的远程服务器上安装 pip 库
- node.js - Dockerized NodeJS 在 npm run 上选择一个脚本
- javascript - React - 需要在关闭浏览器/选项卡时调用 Api
- ios - WKWebView 无法从 ios 13 中的远程 URL 加载 PDF