小编alt*_*ios的帖子

React redux useSelector 和参数

我试图理解为什么我似乎无法将参数传递给选择器。

本质上:我有一个存储数字和布尔值的逻辑 - 该数字是我想要选择材料元素的参数 - 我有一个调度操作的按钮:

//on a jobcard - dispatches to the material logic slice, pushing the number into the redux state: materialforJobNumber;
 <button className="materialViewer"  onClick={() =>{dispatch(materialView(data.jobsessionkey))}}> MATERIAL</button>

Run Code Online (Sandbox Code Playgroud)

它分派到逻辑片:

//logic slice
materialView:(state, action)=>{
    console.log("what");
    console.log("material view succesfully called", action);
    const newState = {...state};
    console.log(newState);
    newState.materialforJobNumber = action.payload;
    newState.showMaterial=true;
    return newState
},
Run Code Online (Sandbox Code Playgroud)
//the component that handles displaying the list of material for a job;

import {selectAttachedByJobKey} from "../MaterialCard/MaterialSlice";
const store= useStore();
const foundJN = store.getState().logic.materialforJobNumber;
const selectedJob=store.getState().jobs.find(x=>x.jobsessionkey==foundJN);
const materials = …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs redux redux-reducers

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

理解react-hooks/exhaustive-deps useEffect和调度事件

我有这个警告:

React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array  react-hooks/exhaustive-deps

Run Code Online (Sandbox Code Playgroud)

用这个代码:

import { useDispatch } from 'react-redux';
import openSocket from "socket.io-client";
import {populateCustomers} from './slices/CustomerSlice';
const ENDPOINT = config.ENDPOINT; //socket address

function App() {
  const dispatch = useDispatch();
  useEffect(() => {
    const socket = openSocket(ENDPOINT);
    //hydrate
    socket.on("hydrateCustomers",data=>dispatch(populateCustomers(data)))

  },[]);
Run Code Online (Sandbox Code Playgroud)

这个想法是,套接字打开一次,并且在服务器发生事件时 - 数据从响应分派到 redux 存储中。

我很确定我想要一个空的依赖项数组,因为我只想运行一次......有没有更好的方法来处理这种情况?或者我应该忽略这个警告?

socket.io reactjs redux react-redux use-effect

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

reactjs:setState 在一次调用的函数中被调用两次?为什么?

编辑:由于代码片段没有重现错误 - 这里是 github repo 的链接:(代码远未完成)

https://github.com/altruios/clicker-game

我现在已经在两台计算机上运行它 - 两者都具有代码片段未显示的相同行为。

//interestingly enough, this works just fine, where the same code I run locally has the doubling.
//when I comment out ALL other code except for this code I STILL get the error locally
//at this point the only difference is import export of components... here they are in one file.
//below is original code from file (
/* 
FILE::::Clicker.js
 
import React from 'react';

function Clicker(props)	
	{
	return(
		<div>
		{props.name}
		<button …
Run Code Online (Sandbox Code Playgroud)

javascript setstate reactjs

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

使用 postman new api 测试 websocket

我正在尝试让 websockets 的邮递员 api 正常工作,但是我什至无法建立连接:

当我尝试建立连接时

ws://localhost:5001/socket.io/

Run Code Online (Sandbox Code Playgroud)

结果是:

Error: Unexpected server response: 400 //for ws
Run Code Online (Sandbox Code Playgroud)
ws://localhost:5001/socket.io/

Run Code Online (Sandbox Code Playgroud)

连接到 socket.io/express/nodejs 服务器的正确 url 字符串是什么?

javascript testing websocket postman

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