为什么不能useEffect()使用 async-await?
const Home: React.FC = () => {
useEffect(async () => {
console.log(await ecc.randomKey())
}, [])
return (
...
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
“() => Promise”类型的参数不可分配给“EffectCallback”类型的参数。
类型 'Promise' 不可分配给类型 'void | (() => void | undefined)'。
类型 'Promise' 不可分配给类型 '() => void | 不明确的'。
类型 'Promise' 不匹配签名 '(): void | 未定义'.ts(2345)
如何使此回叫工作?我已阅读过这些文件,但由于某些原因我无法弄明白?
var ref = new Firebase("https://xxx.firebaseio.com/public/"+$scope.uid+"/shows/");
var blast = ref.child(show_title);
blast.update({
"show_title": show_title,
"show_image": show_image,
"show_description": show_description,
"show_email": show_email,
"time": Firebase.ServerValue.TIMESTAMP
});
blast.update("I'm writing data", function(error) {
if (error) {
alert("Data could not be saved." + error);
} else {
alert("Data saved successfully.");
}
});
Run Code Online (Sandbox Code Playgroud) 如果我在 Node 中读取 wasm 文件,fs.readFileSync我会得到我想要的格式的文件,但是当我尝试使用文件阅读器在浏览器中执行完全相同的操作时,我会以某种方式得到错误的格式?
fs.readFileSyncbut 在浏览器中相当于什么?
是否fs.readFileSync将文件视为数组缓冲区或二进制字符串或其他内容?
<input type="file" onchange="_readWasmFile">
Run Code Online (Sandbox Code Playgroud)
和js
_readWasmFile(event) {
const file = event.target.files[0];
let reader = new FileReader();
reader.readAsBinaryString(file);
reader.onload = () => {
this.sendWasm = reader.result;
};
}
Run Code Online (Sandbox Code Playgroud) 清单和图标取决于浅色/深色模式,当用户更改操作系统中的模式时,有什么方法可以更改它们吗?
我已经触发了事件侦听器
window.matchMedia('(prefers-color-scheme: dark)').addEventListener( "change", (e) => {
if (e.matches) console.log('your in dark mode);
});
Run Code Online (Sandbox Code Playgroud)
但清单是从反应公共文件夹加载的..
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>My Site</title>
</head>
<body>
<noscript>Please enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
不确定如何处理也在公共文件夹根目录中的图标。此外,主题颜色的元标记也需要更改。
我的路由器有一个useReducer并且 initialState 有错误,Argument of type 'StateInterface' is not assignable to parameter of type 'never'.这仅在我action.value在 globalState.tsx 中的减速器中使用时才开始发生
(Router.tsx)
import React, { useReducer } from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import { Layout } from './components/layout';
import { Dispatch, Global, InitialState, Reducer } from './globalState';
import { Home } from './routes/home';
import { NotFound } from './routes/notFound';
const Router: React.FC = () => {
const [global, dispatch] = useReducer(Reducer, InitialState); //<==== Here …Run Code Online (Sandbox Code Playgroud) 正确的类型是什么target: { value: any, name: any }?我得到的错误是Duplicate identifier 'any'.我也得到了错误Binding element 'any' implicitly has an 'any' type.为什么会value给出错误“找不到名称‘值’?”
我这里有一个代码沙箱
const [state, setState] = useState({
fullName: '',
});
const { fullName } = state;
const onChange = ({ target: { value: any, name: any } }) => {
setState((prev) => ({
...prev,
[name] : value, // <= 'Cannot find name 'value'
}));
};
...
<input
type='text'
placeholder='Full name'
name='fullName'
value={fullName}
onChange={onChange}
/>
Run Code Online (Sandbox Code Playgroud) 我得到的错误是Property 'files' does not exist on type 'ChangeEvent<HTMLInputElement>为什么我不能从文件输入访问文件数组?
这是可以使用 useRef 的情况吗?
import React from 'react';
const Photo: React.FC = () => {
const [state, setState] = useState({
photo: '',
});
const {
photo,
} = state;
const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
'ChangeEvent<HTMLInputElement>
event.persist();
setState((prev) => ({
...prev,
[event.target.id]: event.target.value,
}));
console.log(state.photo) // returns nothing
console.log(event.files[0]);
// ^
// Property 'files' does not exist on type
};
return (
<div className='photo'>
<label>
Click Me
<input
type='file'
id='photo' …Run Code Online (Sandbox Code Playgroud) 处理定时器时正确的类型是什么?我曾尝试Timeout和number
let debounceResize: any;
// ^ What should this be?
window.addEventListener('resize', () => {
clearTimeout(debounceResize);
debounceResize = setTimeout(calcCanvasSize, 500);
});
Run Code Online (Sandbox Code Playgroud)
我想拆分Red和red。如何使分割大小写不敏感?
const str = "my Red balloon"
const searchTxt = "red"
const strArr = str.split(searchTxt);
Run Code Online (Sandbox Code Playgroud)
我尝试过的变体
const strArr = str.split(/searchTxt/gi);
Run Code Online (Sandbox Code Playgroud) 我制作了一个组件并将其用作...
<TopNav
loggedIn={global.shared.loggedIn} // false
fullName={global.shared.fullName} // ''
avatar={global.shared.avatarId} // ''
/>
Run Code Online (Sandbox Code Playgroud)
在 TopNav 组件中,我希望能够访问我传入的道具和/props.history或以编程方式导航用户的其他方式而无需刷新..
import React from 'react';
import { RouteComponentProps } from 'react-router-dom';
interface PropsInterfaceNew {
avatar: string;
loggedIn: boolean;
fullName: string;
}
interface PropsInterface
extends RouteComponentProps<PropsInterfaceNew> {}
// ^ Error
const TopNav: React.FC<PropsInterface> = (props: PropsInterface) => {
...
const firstName = props.fullName.split(' ')[0]; // I need props.fullName & others
const search = (event: React.FormEvent) => {
event.preventDefault();
props.history.push('/my/profile'); // I need props.history
};
Run Code Online (Sandbox Code Playgroud)
错误是 …
reactjs ×6
typescript ×6
javascript ×3
async-await ×1
favicon ×1
firebase ×1
node.js ×1
react-hooks ×1