小编Ali*_*gan的帖子

socket.io 连接在生产环境中失败,在 localhost 中工作

我在 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)

sockets node.js express socket.io netlify

6
推荐指数
1
解决办法
4783
查看次数

使用地图反应对象的setState数组不起作用?

我在执行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)

javascript ecmascript-6 reactjs

6
推荐指数
1
解决办法
1207
查看次数

在反应中处理循环内的ref

以下是我的父组件,但是如何选择关注哪个输入?在这种情况下是否必须创建动态引用?

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)

javascript reactjs

4
推荐指数
2
解决办法
4877
查看次数

为什么在redux存储中放置同步状态?

将非异步状态放在redux存储中有什么意义?例如,你有一个模态,显示或没有显示,你想写这么多只是为了切换?将它作为本地状态放在react组件中,并使用setState更新它有什么问题?

我决定哪个州应该通过redux的经验法则是数据是异步的,否则商店里会有这么多的ui状态,有一天它会变得如此之大,你的想法是什么?

javascript flux single-page-application reactjs redux

1
推荐指数
1
解决办法
62
查看次数