【发布时间】:2019-09-11 12:17:35
【问题描述】:
WebContext.js
import React, { createContext, Component } from 'react';
export const WebContext = createContext();
class WebContextProvider extends Component {
state = {
inputAmount: 1,
};
render() {
return <WebContext.Provider value={{ ...this.state }}>{this.props.children}</WebContext.Provider>;
}
}
export default WebContextProvider;
App.js
const App = () => {
return (
<WebContextProvider>
<UpdateBtn />
</WebContextProvider>
);
};
export default App;
更新Btn.js
const UpdateBtn = () => {
return (
<Div>
<Button onClick={} />
</Div>
);
};
export default UpdateBtn;
如何在 UpdateBtn.js 中单击按钮时更新 WebContext.js 中的 inputAmount 状态? App.js 是 UpdateBtn.js 的父组件另外,如何将 WebContext.js 转换为功能组件?
【问题讨论】:
-
在 React 官方文档中有完美的例子:reactjs.org/docs/…
标签: javascript reactjs material-ui jsx