我在 server.js 中有这个
//socket io config
const server = require('http').createServer(app)
const io = require('socket.io')(server)
io.on('connection', function (socket) {
socket.on('SOCKET_COMMENT_RECEIVED', ({ notification }) => {
io.emit(`SOCKET_COMMENT_RECEIVED`, notification)
})
//and many more
})
Run Code Online (Sandbox Code Playgroud)
在我的客户端(反应)
import io from 'socket.io-client'
const socket = io('localhost:3001') // working in localhost
Run Code Online (Sandbox Code Playgroud)
在我的产品中我做了这个检查
let socket = io('localhost:3001')
if(process.env.NODE_ENV === 'production') { socket =
io('https://api.example.com:3001')
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样呢?我不认为这是 cors 问题,因为我已经这样做了
app.use(cors({
origin: true,
credentials: true
}))
Run Code Online (Sandbox Code Playgroud)
我的 package.json 依赖项
"socket.io": "^2.1.1",
"socket.io-client": "^2.1.1",
Run Code Online (Sandbox Code Playgroud) 我在执行setState更改对象嵌套数组的值时遇到问题。下面的代码假设将id 2的问题更改为答案:true,但是没有,这是怎么回事?
this.state = {
questions: [
{
id: 1,
answer: ''
},
{
id: 2,
answer: ''
},
]
}
//I have a click event somewhere
this.setState(
{
questions: this.state.questions.map(q => {
if (q.id === 2) {
return {
...q,
answer: true
}
} else {
return { ...q }
}
})
},
console.log(this.state.questions[1]) // did not see id of 2 being changed to true?
)
Run Code Online (Sandbox Code Playgroud) 以下是我的父组件,但是如何选择关注哪个输入?在这种情况下是否必须创建动态引用?
class TestRef extends React.Component {
ref = React.createRef();
state = {
data: [
{
name: "abc"
},
{ name: "def" }
]
};
focusInput = () => this.ref.current.focus();
render() {
return (
<div>
{this.state.data.map(o => {
return <Hello placeholder={o.name} ref={this.ref} />;
})}
<button onClick={this.focusInput}>focus input 1</button>
<button onClick={this.focusInput}>focus input 2</button>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud) 将非异步状态放在redux存储中有什么意义?例如,你有一个模态,显示或没有显示,你想写这么多只是为了切换?将它作为本地状态放在react组件中,并使用setState更新它有什么问题?
我决定哪个州应该通过redux的经验法则是数据是异步的,否则商店里会有这么多的ui状态,有一天它会变得如此之大,你的想法是什么?
javascript ×3
reactjs ×3
ecmascript-6 ×1
express ×1
flux ×1
netlify ×1
node.js ×1
redux ×1
socket.io ×1
sockets ×1