在这个项目中,我使用React.createContext()方法来管理状态。我可以通过导入Context.Consumer并在类组件(或功能组件)的返回方法中使用此使用者来访问状态数据。请参阅下面的示例:
function App(){
return(
<Consumer>
{value=>{
const {basket} = value;
return <p> { basket.length() } </p>
}
}
</Consumer>
)
}Run Code Online (Sandbox Code Playgroud)
问题是我想从useEffect 挂钩分派一个操作,而不是从组件返回
这就是我想做的:
function App() {
useEffect(() => {
auth.onAuthStateChanged(authUser => {
if(authUser){
dispatch({ //I need to use dispatch methode here!!
type : 'SET_USER',
payload : authUser
})
}
else{
dispatch({ //I need to use dispatch methode here!!
type : 'SET_USER',
payload : null
})
}
})
}, []) …Run Code Online (Sandbox Code Playgroud)我正在编写一个.NET Web应用程序,管理员可以在其中自定义呈现给用户的各种数据输入表单.管理员可以创建和自定义大约六种不同的字段类型(即文本,数字,下拉列表,文件上载).所有字段共享一组基本属性/行为(是否需要该字段?它是否具有默认字段值?).还有一系列字段特定的属性/行为(即下拉列表具有数据源属性,但文本字段不具有).为简单起见,我将遗漏问题域的许多其他特征.
类层次结构很简单:一个抽象的超类,它封装了常见的行为/属性,以及大约六个处理字段特定内容的具体子类.
每个字段类型都呈现(即映射到)作为特定类型的.NET服务器控件,所有这些都派生自System.Web.UI.Control.
我创建了以下代码来映射字段域对象及其相应的UI控件之间的值:
public static void Bind(Control control, IList<DocumentFieldBase> fieldBaseList)
foreach (DocumentFieldBase fieldBase in fields){
if (typeof (DocumentFieldText).IsInstanceOfType(fieldBase)){
TextBox textbox = (TextBox) control;
textbox.Text = (fieldBase as DocumentFieldText).GetValue();
}
if (typeof (DocumentFieldDropDown).IsInstanceOfType(fieldBase)){
DropDown dropDown= (DropDown) control;
dropDown.Text = (fieldBase as DocumentFieldSelectOne).GetValue().Text;
dropDown.DataSource= (fieldBase as DocumentFieldSelectOne).DataSource;
dropDown.Id= (fieldBase as DocumentFieldSelectOne).GetValue().Id;
}
//more if statements left out for brevity
}
}
Run Code Online (Sandbox Code Playgroud)
如果执行类型检查的语句,我想抛弃那些不敬虔的人.我正在拍摄的方法是使用子类输入为每个字段/控件组合创建一个方法重载.例如:
public static void Bind(TextBox control, DocumentFieldText fieldText){
//some implementation code
}
public static void Bind(DropDown control, DocumentFieldDropDown fieldDropDown){ …Run Code Online (Sandbox Code Playgroud) 我对前台调度行为有一个恼人的问题。有时onNewIntent()它会完全重新创建 Activity而不是调用,这会破坏应用程序的工作流程。
我的具体情况:Activity A是MainActivity,使用前台调度。一切正常。但是,在我从浏览器启动的活动 B(VIEW 操作)中,前台调度在某些情况下不再起作用。
工作流程:
我启动 MainActivity,切换到浏览器(不关闭 MainActivity),启动 Activity B 并连接我的 NFC 设备 --> 它创建了一个新的 Activity B。
我启动 MainActivity 并再次关闭它。之后我切换
到浏览器,启动活动 B 并连接我的 NFC 设备 --> 一切正常onNewIntent()
代码是正确的,例如,如果我在第一个场景中两次连接 NFC 设备,它会在第二次正常工作,但在第一次不正常。在 MainActivity 和活动 BI 中明确调用活动的 onPause() 方法中的 disableForegroundDispatch() 方法。
我的具体问题有解决方案吗?对我来说,这听起来像是一个错误。
编辑:
public void resume(Activity targetActivity) {
if (nfc != null && nfc.isEnabled()) {
// nfc is the default NFC adapter and never null on my devices
Intent intent = new Intent(targetActivity, targetActivity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = …Run Code Online (Sandbox Code Playgroud) 我正在学习 python 的 win32com ,我遇到了一个奇怪的问题。
我正在尝试导出字典列表中的 Outlook 联系人。我的代码与 win32com.client.Dispatch("Outlook.Application) 完美配合。但它使用 win32com.client.gencache.EnsureDispatch("Outlook.Application) 返回 0 个联系人,这应该更快、更“安全”。这是我的代码:
class MapiImport():
def __init__(self):
self.olApp = win32com.client.Dispatch("Outlook.Application")
self.namespace = self.olApp.GetNamespace(u"MAPI")
# olFolderContacts = 10 :
self.mapiContacts = self.namespace.GetDefaultFolder(10).Items
def getContacts(self, *fields):
contacts = []
# Class == 40 is ContactItem
# Class == 69 is DistListItem
# Exclude ditribution list and others objects != ContactItem
for contact in filter(lambda x: x.Class == 40,self.mapiContacts) :
if not fields :
ctact = dict((x.Name,x.Value) for x in contact.ItemProperties)
else …Run Code Online (Sandbox Code Playgroud) 我编写了一个 bash 脚本,它将命令作为第一个位置参数,并使用 case 构造作为类似于以下内容的调度:
do_command() {
# responds to invocation `$0 command ...`
}
do_copy() {
# respond to invocation: `$0 copy...`
}
do_imperative() {
# respond to invocation: `$0 imperative ...`
}
cmd=$1
shift
case $cmd in
command)
do_command $*
;;
copy)
do_copy $*
;;
imperative)
do_imperative $*
;;
*)
echo "Usage: $0 [ command | copy | imperative ]" >&2
;;
esac
Run Code Online (Sandbox Code Playgroud)
该脚本根据调用决定哪个函数$1,然后将剩余的参数传递给该函数。我想在不同的部分匹配上添加能力调度,但我想以一种优雅的方式做到这一点(优雅的定义是一种既易于阅读又不会冗长到碍眼或分散注意力的方式)。
明显有效(但不优雅)的解决方案可能是这样的:
case $cmd in
command|comman|comma|comm|com)
do_command $*
;;
copy|cop)
do_copy $*
;; …Run Code Online (Sandbox Code Playgroud) 我想在不同的场合分派 CustomEvent。网络上的大多数示例只创建和分派 CustomEvents 一次。我想知道如何正确地做到这一点。
在这个例子中,“A-Button”一遍又一遍地分派相同的事件。“B-Button”new CustomEvent每次创建一个,应该调度事件。
var targetA = document.getElementById('targetA');
var targetB = document.getElementById('targetB');
var evtA = new CustomEvent("myEvent");
function a(){
targetA.dispatchEvent(evtA);
}
function b(){
targetB.dispatchEvent(new CustomEvent("myOtherEvent"));
}
targetA.addEventListener("myEvent", function(e){
console.log(e);
console.log(e.timeStamp);
});
targetB.addEventListener("myOtherEvent", function(e){
console.log(e);
console.log(e.timeStamp);
});Run Code Online (Sandbox Code Playgroud)
<button onclick="a()">A</button>
<button onclick="b()">B</button>
<div id="targetA"></div>
<div id="targetB"></div>Run Code Online (Sandbox Code Playgroud)
A 方法的副作用可能是时间戳未更新。这可能会导致依赖时间戳的处理程序出现意外行为。
B 方法的性能可能较低,因为 CustomEvent 对象被一遍又一遍地实例化。(内存应该不是问题。)
有没有“正确”的方法,或者有什么最佳实践?
也许我不了解 Laravel 队列的工作原理,或者它本身不起作用,我对 Laravel 队列/调度的预期行为是,如果从控制器启动调度,则调度到队列的代码应静默执行,并在背景。最终用户浏览器不应等待代码执行。
然而,这就是我的代码发生的情况,调度到队列的代码在执行时让浏览器“旋转...”。
这是预期的行为吗?编码:
**Controller:**
public function make_eps_certs($tbl_eps)
{
//dd(Carbon::now()->addMinutes(10))
Log::info('Dispatching maeEPSCert to Queue');
$var_result=makeEPSCerts::dispatch($tbl_eps)->onQueue('eventadmin')
->delay(10);
return redirect()->back();
}
**Job:**
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\partSubs;
use Log;
use Image;
class makeEPSCerts implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
protected $passdata;
public $timeout = 120;
public function __construct($passdata)
{
Log::info('Constructing makeEPSCert');
$this->passdata = $passdata;
}
/** …Run Code Online (Sandbox Code Playgroud) 我有一个在向量上定义的大函数,但我希望它也可以使用单个值。我希望第一个参数的类型是向量或数字。
我尝试以下方法:
function bigfunction(x::Vector, y::Float64=0.5)
# lots of stuff
z = x .+ y
return z
end
bigfunction(x::Number) = bigfunction()
Run Code Online (Sandbox Code Playgroud)
该函数适用于向量,但不适用于数字。
bigfunction([0, 1, 3])
bigfunction(2)
Run Code Online (Sandbox Code Playgroud)
Union{}我应该像有时看到的那样做一些事情吗?或者以不同的方式重新定义方法?
借助 DOM,您可以轻松地使用 Javascript 创建触发自定义事件,如下所示:
var event = new Event('build');
// Listen for the event.
elem.addEventListener('build', function (e) { /* ... */ }, false);
// Dispatch the event.
elem.dispatchEvent(event);
Run Code Online (Sandbox Code Playgroud)
有没有办法用 React-Native 做到这一点?
我是 Rust 的新手,我正在尝试了解该语言的基础知识。
\n\n考虑以下特征
\n\ntrait Function {\n fn value(&self, arg: &[f64]) -> f64;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n和两个实现它的结构:
\n\nstruct Add {}\n\nstruct Multiply {}\n\nimpl Function for Add {\n fn value(&self, arg: &[f64]) -> f64 {\n arg[0] + arg[1]\n }\n}\n\nimpl Function for Multiply {\n fn value(&self, arg: &[f64]) -> f64 {\n arg[0] * arg[1]\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n在我的main()函数中,我想将Add\xc2\xa0and的两个实例分组Multiply到一个向量中,然后调用该value方法。以下作品:
fn main() {\n let x = vec![1.0, 2.0];\n let funcs: Vec<&dyn Function> = vec![&Add {}, &Multiply …Run Code Online (Sandbox Code Playgroud) dispatch ×10
javascript ×2
polymorphism ×2
.net ×1
android ×1
bash ×1
c# ×1
command ×1
consumer ×1
events ×1
foreground ×1
function ×1
julia ×1
laravel ×1
laravel-5.5 ×1
methods ×1
nfc ×1
outlook ×1
overloading ×1
python ×1
pywin32 ×1
queue ×1
react-native ×1
reactjs ×1
reducers ×1
rust ×1
traits ×1
triggers ×1
win32com ×1