reactjs - 从 export const 调用类函数 react native
问题描述
我想从 webViewEventHandlers.js 中的 export const 类调用 ComposeBox.js 中的 handleSend 函数
ComposeBox.js
class ComposeBox extends PureComponent<Props, State> {
handleSend = () => {
const { dispatch } = this.props;
const { message } = this.state;
dispatch(addToOutbox(this.getDestinationNarrow(), message));
this.setMessageInputValue('');
};
export default connect((state: GlobalState, props) => ({
auth: getAuth(state),
}))(ComposeBox);
webViewEventHandlers.js
import ComposeBox from '../compose/ComposeBox';
export const handleMessageListEvent = () => {
case 'wizrep':
ComposeBox.handleSend();
break;
我收到以下错误
解决方案
React 手册指出:所有 React 组件在其 props 方面都必须像纯函数一样工作。
这意味着尝试创建 Component 的实例并将其视为对象会给您带来非常困难的时间。
根据您发送的代码片段,我可以想象一些可能的解决方法:
- 从 ComposeBox 内部调用 handleMessageListEvent,将 handleSend 函数作为参数传递。
- 将消息设置为 Redux 状态的一部分而不是组件,并从 webViewEventHandlers 调度操作。
- 使 handleMessageListEvent 成为 ComposeBox 组件的一部分。
推荐阅读
- java - CRUD 存储库的所有方法都返回 null
- reactjs - 如何适应边界和中心反应谷歌地图
- python - 属性错误:“嵌入”对象没有属性“嵌入”-TensorFlow 和 Keras
- c++ - 将带空格的文本转换为驼峰式
- python - 如何在matplotlib中重新定位x轴以设置为0
- reactjs - 尝试导入错误:“createNote”未从“./graphql/mutations”导出(导入为“createNoteMutation”)
- ext.net - Ext.NET Combobox 本地查询不返回数据时的回调
- tensorflow - 在'tensorflow 中无法获取'日志'
- vb6 - 打开VB6项目时FM20.dll和crystl32.ocx的问题
- node.js - firebase 函数日期查询