我有以下代码(服务器):
import express from "express";
import socketio from "socket.io";
import http from "http";
const app = express();
const server = http.createServer(app);
const io = socketio(server);
server.listen(process.env.port || 3000, () => {
console.log(`App running on port ${process.env.port || 3000}`);
});
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误const io = socketio(server);,它指出:
此表达式不可调用。类型 'typeof import("SOME_PATH/node_modules/socket.io/dist/index")' 没有调用签名
这里究竟有什么问题?
包.json:
"devDependencies": {
"@types/express": "^4.17.11",
"@types/node": "^14.14.27",
"@types/socket.io": "^2.1.13",
"nodemon": "^2.0.7",
"ts-node": "^9.1.1"
},
"dependencies": {
"express": "^4.17.1",
"socket.io": "^3.1.1",
"typescript": "^4.1.5"
}
Run Code Online (Sandbox Code Playgroud) 路由到这样的组件有什么区别:
<Route path="coolPath" component={MyComponent} />
or
<Route path="coolPath" render={props => <MyComponent {...props} customProp="s" } />
Run Code Online (Sandbox Code Playgroud)
对此:
<Route path"=coolPath">
<MyComponent />
</Route>
or
<Route path"=coolPath">
<MyComponent cusomProps="cp"/>
</Route>
Run Code Online (Sandbox Code Playgroud) 我有这个简单的代码:
const [state, setState] = useState([]);
useEffect(() => {
socket.on('something', data => {
console.log('ONE');
setState(old => {
console.log('TWO');
const newArr = [...old];
// do something to newArr
return newArr;
});
});
return () => {
socket.off('something');
};
}, []);
Run Code Online (Sandbox Code Playgroud)
一切都按预期工作,但由于某种原因something回调触发一次(ONE打印一次),但在我设置状态时,setState回调被调用两次(打印TWO两次)。这是为什么?
我有一个反应功能组件:
function Chat(props) {
const [messages, setMessages] = useState([]);
const inputRef = useRef();
//The socket is a module that exports the actual socket.io socket
socket.socket.on("chatMessage", (msg) => {
setMessages([...messages, msg]);
});
const inputChanged = (e) => {
if (e.key === "Enter") {
socket.sendMessage(inputRef.current.value)
.then(() => {
//do something
})
.catch(console.log);
inputRef.current.value = "";
}
};
return (
<div>
{messages.map((msg, i) => <ChatMessage key={i}>{msg}</ChatMessage>)}
<input ref={inputRef} onKeyPress={inputChanged} />
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
但是当我从 更新状态时socket.socket.on("chatMessage",出现错误
无法对已卸载的组件执行 React 状态更新
套接字告诉我需要很长时间才能响应,并且开始发生一些递归。
我应该如何从套接字事件更新组件状态?
我有一个名为a的基类,它有一个名为Foo的虚函数
class a
{
public virtual void Foo() {}
}
Run Code Online (Sandbox Code Playgroud)
我还有很多继承自它的其他类.
class B : A{}
class C : A{}
class D : A{}
class E : A{}
Run Code Online (Sandbox Code Playgroud)
现在,我希望有一个类型的数组,所以我可以随机选择一个所以我试过这个:
class Boo
{
Type[] options;
public Boo()
{
options = new[]
{
typeof(B),
typeof(C),
typeof(D),
typeof(E)
};
}
}
Run Code Online (Sandbox Code Playgroud)
然后我想随机选择一个并使用它的Foo方法,我这样做:
Random rnd = new Random();
(options[rnd.Next(options.Length)] as A).Foo()
Run Code Online (Sandbox Code Playgroud)
但这不起作用,有没有办法实现这一点?
(顺便说一句,我没有这个名字,所以如果有人有更好的名字,他们可以随意编辑:))
我想使用这个类中的const变量:
class foo
{
public const bool x = true;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在不做foo.x的情况下使用x?
我想像这样使用它:
if( x )
Run Code Online (Sandbox Code Playgroud)
而不是这样的:
if( foo.x )
Run Code Online (Sandbox Code Playgroud) reactjs ×3
socket.io ×3
c# ×2
javascript ×2
types ×2
callback ×1
class ×1
inheritance ×1
node.js ×1
react-hooks ×1
react-router ×1
routes ×1
typescript ×1