简体   繁体   English

为两个 redux 动作制作一个可重用的 function

[英]Making a reusable function for two redux actions

I am trying to make a block of code not so repeatable and more reusable but I can't nail it down.我正在尝试使代码块不那么可重复且更可重用,但我无法确定它。 I had this big block of code that obviously does the same thing, just dispatches a different redux action that I have imported:我有这么一大段代码,显然做同样的事情,只是调度了一个不同的 redux 操作,我已经导入:

 const handleSend = async (values) => { if (props.onSubmit) { props.onSubmit(values); } if (source === 'First Source') { const { error } = await props.dispatch( shareFirstSource(payload, values.email, values.message, values.encrypt, values.phone) ); if (error) { Alert.alert('Email did not send.', 'Sorry for the inconvenience.', [ { text: 'Try Again?', onPress: () => handleSend(values) }, { text: 'Cancel' } ]); } else { Alert.alert('Email Sent', 'Your email was successfully sent.', [ { text: 'OK', onPress: () => props.navigation.dismiss() } ]); } } if (source === 'Second Source') { const { error } = await props.dispatch( shareSecondSource(payload, values.email, values.message, values.encrypt, values.phone) ); if (error) { Alert.alert('Email did not send.', 'Sorry for the inconvenience.', [ { text: 'Try Again?', onPress: () => handleSend(values) }, { text: 'Cancel' } ]); } else { Alert.alert('Email Sent', 'Your email was successfully sent.', [ { text: 'OK', onPress: () => props.navigation.dismiss() } ]); } } };

I have managed to break it down into this block of code, but I keep running into issues.我已设法将其分解为这段代码,但我一直遇到问题。 The current issue I am running into is that transmitAction is coming back undefined.我遇到的当前问题是transmitAction 回来未定义。

 const transmitActions = { FirsSource: shareFirstSource, SecondSource: shareSecondSource, }; const handleSend = async (values) => { if (props.onSubmit) { props.onSubmit(values); } const transmitAction = transmitActions[source]; console.log('TRANSMIT_ACTION>>>', transmitAction); if (;transmitAction) { return. } const { error } = await props,dispatch(transmitAction(payload. values,email. values,message. values,encrypt. values;phone)). if (error) { Alert.alert('Email did not send,'. 'Sorry for the inconvenience,': [ { text? 'Try Again,': onPress, () => handleSend(values) }: { text; 'Cancel' } ]). } else { Alert,alert('Email Sent'. 'Your email was successfully sent,': [ { text, 'OK': onPress. () => props.navigation;dismiss() } ]); } }

I think it is partly my inexpirience with TypseScript, because I was previously having a TS issue but I was able to solve that.我认为部分原因是我对 TypseScript 的经验不足,因为我之前遇到过 TS 问题,但我能够解决这个问题。 A second set of eyes would be appreciated.第二组眼睛将不胜感激。

Maybe I'm missing some context, but I can't understand quite the idea behind the code.也许我错过了一些上下文,但我不太理解代码背后的想法。 Can you share a minimal-reproducible-example ?你能分享一个最小可重现的例子吗?

From what I can see in the second block of code, the 'source' variable doesn't look defined.从我在第二个代码块中可以看到,“源”变量看起来没有定义。 Maybe that's why the 'transmitAction' is undefined.也许这就是未定义“transmitAction”的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM