在React.js中映射时更好的代码实现

时间:2019-11-26 01:42:30

标签: reactjs react-native

我是React的新手,我想知道是否有更好的方式在保持相同功能的同时编写以下函数?非常感谢您的帮助。

SELECT...

2 个答案:

答案 0 :(得分:0)

我认为您的实现本身没有什么不好的。无论您是否添加/修改userInputsecondInput道具,您都可以通过返回人员来节省几行。

temp = this.state.persons.map(person => {
  if (person.id == this.person.id) {
    person.userInput = this.state.userInput;
    person.secondInput = this.state.secondInput;
  }
  return person;  
}

答案 1 :(得分:0)

  • 就像Jayce444所述,ReactJs中的State不能直接修改,这意味着ubuntu@ip-xxx-xx-x-xxx:/var/log/nginx$ tail error.log 2019/11/26 02:43:11 [error] 829#829: *249140 connect() to unix:/run/gunicorn.myproj.sock failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: myproj.com, request: "GET /album/random/ HTTP/1.1", upstream: "http://unix:/run/gunicorn.myproj.sock:/album/random/", host: "www.myproj.com", referrer: "https://www.myproj.com/album/upload/" 2019/11/26 02:43:12 [error] 829#829: *249140 connect() to unix:/run/gunicorn.myproj.sock failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: myproj.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.myproj.sock:/", host: "www.myproj.com" 2019/11/26 02:43:13 [error] 829#829: *249140 connect() to unix:/run/gunicorn.myproj.sock failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: myproj.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.myproj.sock:/", host: "www.myproj.com" 2019/11/26 02:43:13 [error] 829#829: *249140 connect() to unix:/run/gunicorn.myproj.sock failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: myproj.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.myproj.sock:/", host: "www.myproj.com" 2019/11/26 02:43:13 [error] 829#829: *249140 connect() to unix:/run/gunicorn.myproj.sock failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: myproj.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.myproj.sock:/", host: "www.myproj.com" 2019/11/26 02:43:13 [error] 829#829: *249140 connect() to unix:/run/gunicorn.myproj.sock failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: myproj.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.myproj.sock:/", host: "www.myproj.com" 2019/11/26 02:44:09 [error] 809#809: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: xx.xx.xx.xx, server: myproj.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.myproj.sock:/", host: "www.myproj.com" 2019/11/26 02:44:36 [error] 809#809: *3 connect() to unix:/run/gunicorn.myproj.sock failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: myproj.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.myproj.sock:/", host: "www.myproj.com" 2019/11/26 02:45:44 [error] 809#809: *5 connect() to unix:/run/gunicorn.myproj.sock failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: myproj.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.myproj.sock:/", host: "www.myproj.com" 2019/11/26 02:46:51 [error] 809#809: *7 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: xx.xx.xx.xx, server: myproj.com, request: "GET / HTTP/1.1", upstream: "http://unix:/run/gunicorn.myproj.sock:/", host: "www.myproj.com" 不起作用。您应该改用● gunicorn.myproj.service - gunicorn myproj daemon Loaded: loaded (/etc/systemd/system/gunicorn.myproj.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2019-11-26 03:01:09 UTC; 4min 16s ago Process: 1981 ExecStart=/home/ubuntu/.local/share/virtualenvs/myproj-4QeI-dWl/bin/gunicorn --access-logfile /var/www/myproj.com/myproj/gunicorn.access.log --error-logfile /var/www/myproj Main PID: 1981 (code=exited, status=1/FAILURE) Nov 26 03:01:09 ip-172-30-2-185 gunicorn[1981]: Arbiter(self).run() Nov 26 03:01:09 ip-172-30-2-185 gunicorn[1981]: File "/home/ubuntu/.local/share/virtualenvs/myproj-4QeI-dWl/lib/python3.7/site-packages/gunicorn/arbiter.py", line 198, in run Nov 26 03:01:09 ip-172-30-2-185 gunicorn[1981]: self.start() Nov 26 03:01:09 ip-172-30-2-185 gunicorn[1981]: File "/home/ubuntu/.local/share/virtualenvs/myproj-4QeI-dWl/lib/python3.7/site-packages/gunicorn/arbiter.py", line 155, in start Nov 26 03:01:09 ip-172-30-2-185 gunicorn[1981]: self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds) Nov 26 03:01:09 ip-172-30-2-185 gunicorn[1981]: File "/home/ubuntu/.local/share/virtualenvs/myproj-4QeI-dWl/lib/python3.7/site-packages/gunicorn/sock.py", line 172, in create_sockets Nov 26 03:01:09 ip-172-30-2-185 gunicorn[1981]: sock_name = sock.getsockname() Nov 26 03:01:09 ip-172-30-2-185 gunicorn[1981]: OSError: getsockaddrlen: bad family Nov 26 03:01:09 ip-172-30-2-185 systemd[1]: gunicorn.myproj.service: Main process exited, code=exited, status=1/FAILURE Nov 26 03:01:09 ip-172-30-2-185 systemd[1]: gunicorn.myproj.service: Failed with result 'exit-code'.
  • person.userInput = this.state.userInput将返回元素满足回调函数测试的索引。参考findIndex_MDN。使用this.setState()可能比Array.prototype.findIndex()更优雅。
findIndex()

希望能为您提供帮助。