我不喜欢调用单独传递参数的函数,而是更喜欢将它们作为对象传递,这样顺序并不重要。
例如
const sum = ({ arg1, arg2, arg3 }) => arg1 + arg2 + arg3;
Run Code Online (Sandbox Code Playgroud)
现在,我正在将一些代码迁移到 Typescript,但我不确定应该如何为这样的函数定义接口。
我已经尝试过类似的方法,但它不起作用:
有什么线索吗?
我的 dotenv 包有问题。
我的应用程序文件夹:
|_app_folder
|_app.js
|_password.env
|_package.json
Run Code Online (Sandbox Code Playgroud)
我当然安装了 dotenv,但是当我尝试记录 process.env 变量时,结果总是未定义,请您帮帮我吗?
密码.env:
//password.env
CLIENT_ID=xxxxxxxx
Run Code Online (Sandbox Code Playgroud)
应用程序.js:
//app.js
const express = require('express');
const app = express();
const Twig = require("twig");
//Require dotenv
require('dotenv').config();
// Setting the Twig options
app.set("twig options", {
allow_async: true,
strict_variables: false
});
app.get('/', function (req, res) {
//Trying to log it
console.log(process.env.CLIENT_ID);
//
res.render('index.twig', {
date : new Date().toString()
});
});
app.get('/instagram',function(req,res){
// Building the URL
let url = 'https://api.instagram.com/oauth/authorize/?client_id=';
// Redirect to instagram for …Run Code Online (Sandbox Code Playgroud) 我正在尝试为solid-js 制作一个自定义“钩子”,它将从本地存储中检索状态。
import { Accessor, createSignal, Setter } from "solid-js";
export default function createLocalStorageSignal<T extends string>(key: string): [get: Accessor<T>, set: Setter<T>] {
const storage = window.localStorage;
const initialValue: T = JSON.parse(storage.getItem(key) ?? '{}').value;
const [value,setValue] = createSignal<T>(initialValue);
const newSetValue: Setter<T> = (newValue) => {
setValue(newValue);
storage.setItem(key, JSON.stringify({value: newValue}));
return newValue;
}
return [
value,
newSetValue
]
}
Run Code Online (Sandbox Code Playgroud)
但是我收到类型错误
Type '(newValue: any) => void' is not assignable to type 'Setter<T>'
Run Code Online (Sandbox Code Playgroud)
为什么不能推断 newValue 的类型?如果无法推断,我应该将其设置为什么?
编辑:
Setter<T>的完整类型是
type Setter<T> = undefined extends …Run Code Online (Sandbox Code Playgroud) 我相信 Solid 和 Svelte 的工作方式几乎相同,都使用反应图进行计算。
不同之处在于 Solid 使用函数以显式方式执行此操作,而 Svelte 执行相同的操作,但使用特殊的语法。
但它们在概念上和内部上几乎相同吗?
React的useState和Solid JS的createSignal有什么区别?
Solid 的信号比 React 的状态有优势吗?
import React, { useState } from "react";
function App() {
const [counter, setCounter] = useState(0);
return (
<div>
<p>{counter}</p>
<button onClick={() => setCounter((counter) => counter + 1)}>
Click Me
</button>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
import React, { useState } from "react";
function App() {
const [counter, setCounter] = useState(0);
return (
<div>
<p>{counter}</p>
<button onClick={() => setCounter((counter) => counter + 1)}>
Click Me
</button>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
哪一种在性能和心智模型方面更好?
我在 SolidJS 中有一个组件,看起来像这样:
const MyComponent: Component = (params) => {
// ...
const [votes, setVotes] = createSignal(new Set());
const toggle = (val: string) => {
if (votes().has(val)) {
let _votes = votes();
_votes.delete(val);
setVotes(_votes);
} else {
let _votes = votes();
_votes.add(val);
setVotes(_votes);
}
}
return <>
<For each={someArray()}>{(opt, i) =>
<button
style={{
color: `${ votes().has(opt) ? "blue" : "black"}`
}}
onClick={() => { toggle(opt) } }
>
{opt()}
</button>
}</For>
</>
}
Run Code Online (Sandbox Code Playgroud)
我想要发生的是,按钮的样式将根据它(opt在 for 循环中)是否存在于集合中而改变votes。 …
我正在遵循教程中的上下文示例,我从示例中了解到的是使用自定义提供程序:
import { createSignal, createContext, useContext } from "solid-js";
const CounterContext = createContext();
export function CounterProvider(props) {
const [count, setCount] = createSignal(props.count || 0),
counter = [
count,
{
increment() {
setCount((c) => c + 1);
},
decrement() {
setCount((c) => c - 1);
},
},
];
return (
<CounterContext.Provider value={counter}>
{props.children}
</CounterContext.Provider>
);
}
export function useCounter() {
return useContext(CounterContext);
}
Run Code Online (Sandbox Code Playgroud)
我有三个问题:
除了上面的示例之外,我找不到任何有关如何定义自定义上下文提供程序的规范,是否有任何标准或规范可供遵循?
此示例中 CounterContext 和 CounterProvider 之间的绑定在哪里?是在这一行吗?<CounterContext.Provider value={counter}>。与createSignal结合然后在计数器中使用?
所以依赖关系是:createSignal->counter->CounterProvider?
在示例中,他们使用正则表达式的选择器发出更新:
db.history.updateOne({ "_id": /^7000000_/, "count": { $lt: 1000 } },
{
"$push": {
"history": {
"type": "buy",
"ticker": "MDB",
"qty": 25,
"date": ISODate("2018-11-02T11:43:10")
} },
"$inc": { "count": 1 },
"$setOnInsert": { "_id": "7000000_1541184190" }
},
{ upsert: true })
Run Code Online (Sandbox Code Playgroud)
我试图在 Rust 中做同样的事情,但查询将我的正则表达式解释为字符串文字,而不是评估正则表达式。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RepetitionBucketUpdate {
#[serde(with = "serde_regex")]
id: Regex,
device_id: Uuid,
session_id: Uuid,
set_id: Uuid,
exercise: String,
level: String,
count: mongodb::bson::Bson,
}
impl From<JsonApiRepetition> for RepetitionBucketUpdate {
fn …Run Code Online (Sandbox Code Playgroud)