我正在开发一个反应组件。要求是在对象数组中的备用位置添加一个新对象,如下所示:
arr1 = [{test: 1},{test: 2},{test: 3},{test: 4}]
Run Code Online (Sandbox Code Playgroud)
预期输出:
arr1 = [{test: 1},{dummy:1},{test: 2},{dummy:1},{test: 3},{dummy:1},{test: 4}]
Run Code Online (Sandbox Code Playgroud)
有没有办法在 es6 中做同样的事情?
根据 Redux Style Guide,强烈建议连接更多组件来从 store 读取数据。
例如,不只是连接一个
<UserList>组件并读取整个用户数组,而是<UserList>检索所有用户 ID 的列表,将列表项呈现为<UserListItem userId={userId}>,并<UserListItem>连接并从存储中提取其自己的用户条目。
不过,这听起来有点与前面“React 的使用”部分所鼓励的内容相矛盾,即将展示组件与容器组件分开,其中展示组件从 props 中读取数据,而不是从 store 中读取数据。
这是否意味着:
我是 Next JS 的新手。
我使用Next JS和Redux。
我有一个简短的代码如下:
const AdminContainer = (props) => {
return (
<AdminMasterView>
<DashboardView studentList={props.studentListServer}/>
</AdminMasterView>
)
}
export const getStaticProps = (async () => {
let response = await db.getInstance().query('SELECT * FROM student_register;');
return {
props: {
studentListServer: response
}, // will be passed to the page component as props
}
})
const mapStateToProps = state => ({
studentList: state.studentInfoReducers.studentList
});
const mapDispatchToProps = {
getStudentRegisterAction
};
export default connect(mapStateToProps, mapDispatchToProps)(AdminContainer); …Run Code Online (Sandbox Code Playgroud) 如果我使用 setInterval(line 15) 而不使用 useEffect ,那么它给出的结果是 2^n-1(0,1,3,7,15,31,63...) 而不是(0,1,2,3,4, ..)。所以我有一些问题
1)为什么当我直接调用setInterval而不使用useEffect时我会得到该输出2)如果我更改setCount(第9行)并且它通过直接使用setInterval而不使用useEffect给出正确的输出(就像我所做的那样)有什么办法3)如果没有useEffcet就不可能使用setInterval,那么为什么不可能呢?
如果我将 setInterval 放入 useEffect 中并最初渲染一次(第 12,13,14 行),那么它会给出正确的输出......但是当我直接使用 setInterval 时,我没有得到正确的输出。什么是差异赌注?
在这两种情况下,我都调用 setInterval 一次,但输出是 diff。
import React, {useEffect, useState } from 'react'
export default function IncorrectDependency() {
const [count,setCount]=useState(0)
const inc=()=>{
// console.log(count)
setCount(preVal=>preVal+1)
// setCount(count+1)
}
// useEffect(()=>{
// setInterval(inc,1000)},[]
// )
setInterval(inc,1000)
return (
<div>
<h1>{count}</h1>
</div>
)
}
Run Code Online (Sandbox Code Playgroud) 我的应用程序是这样的:
盖茨比计划
index.tsx - 应用程序的入口点 - 我调用
<Provider store={store}>
<HomePage />
</Provider>
Run Code Online (Sandbox Code Playgroud)
HomePage.js 用途mapStateToProps与用途connect(mapStateToProps)(HomePage)
当然,在我的主页中,我使用了多个 React 组件。我可以利用该商店,读取/写入该商店上发生的任何事情HomePage。
在我的主页上,我有指向 的链接ActivityPage。一旦我点击此页面并登陆那里 - 问题就开始了。
ActivityPage 使用多个子组件来完成任务。如何从 ActivityPage 访问读/写?
我无法useDispatch在商店中写入甚至读取。我可以在我的 ReactDev 工具上看到该商店,但在尝试读/写时,我收到 11 个错误,大部分是沿着我需要用以下内容包装组件的思路:<Provider>
我在 Stack Overflow 上阅读了不同的解决方案,这表明我需要拥有mapStateToProps并使用connect需要访问商店的任何组件。
我使用 redux 编写了一个简单的添加到购物车功能。当我尝试将产品添加到数组时,它会抛出错误:
state.products.push 不是一个函数。
初始状态
export const INITIAL_STATE = {
products: []
};
Run Code Online (Sandbox Code Playgroud)
我使用 redux-sauce 来调度该操作。
减速器
export const AddToCart = (state, { item }) => ({
...state,
products: state.products.push(item),
});
Run Code Online (Sandbox Code Playgroud) 我正在为 axios (React Native) 设置全局标头,因此我需要从 redux 存储中获取令牌。但是当我尝试获取它时,它会抛出错误:无效的挂钩调用
任何解决方法或解决方案都会有很大帮助!
import { useSelector } from 'react-redux';
let authToken;
function getToken() {
authToken = useSelector(state => state?.authReducer?.authToken);
return;
} getToken();
Run Code Online (Sandbox Code Playgroud) 我的store.js:
import { configureStore } from "@reduxjs/toolkit";
import userReducer from "../features/userSlice";
export default configureStore({
reducer: {
user: userReducer,
},
});
Run Code Online (Sandbox Code Playgroud)
我应该导入什么index.js?
表明:Attempted import error: 'configureStore' is not exported from './app/store'
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import { configureStore } from "./app/store";
import { Provider } from "react-redux";
ReactDOM.render(
<React.StrictMode>
Provider configureStore={configureStore}>
<App/>
</Provider>
</React.StrictMode>,
document.getElementById("root")
);
Run Code Online (Sandbox Code Playgroud) 我正在使用 React Native/Firebase/Redux 构建一个简单的登录系统。我正在尝试找出如何捕获由于登录尝试失败而发生的错误。
这是我的 authscreen.js:
const [alertShowing, setAlertShowing] = useState(false);
const [alertMessage, setAlertMessage] = useState('');
...
function handleLogin() {
const response = dispatch(login(email, password));
console.log(response);
}
Run Code Online (Sandbox Code Playgroud)
动作.js:
export const login = (email, password) => {
return async (dispatch) => {
try {
const response = await Firebase.auth().signInWithEmailAndPassword(email, password);
dispatch(getUser(response.user.uid));
} catch (e) {
return e;
}
};
};
Run Code Online (Sandbox Code Playgroud)
上面的 console.log(response) 正确地向我显示了错误消息,但这显然对用户来说不是很有用。另请注意,使用正确的凭据我可以正确登录。
我真正想做的handleLogin()是检查响应是否是错误,如果是,setlAlertShowing(true)以及setAlertMessage我从 useDispatch 挂钩返回的内容,以便我可以很好地向用户显示它。
我该怎么办?TIA。
几天前我开始学习 RTK Query,我一直喜欢它很酷的功能和简单性,所以我决定在我使用 Next.js 构建的项目中从 useContext 切换到 RTK Query 以及使用 Node.js 和 Express 的自定义服务器。在这个项目中,我制作了一个用于登录和注册的 api 路由,在提供的自定义 axios 基本查询 RTK 查询的帮助下,可以使用 RTK 查询和 Axios 来实现该路由。登录和注册 api 端点已经有一个在 cookie 存储中存储令牌的逻辑。我将 RTK 查询与 axios 一起使用来发布用户请求,以便他们可以获得 cookie 存储中的令牌存储的响应。这种将用户令牌存储在 cookie 中的逻辑与 useContext 和 axios 配合得很好。但使用 RTK 查询时逻辑并未按预期工作,结果如下:
export const fetcherApi = createApi({
reducerPath: "fetcherApi",
baseQuery: …Run Code Online (Sandbox Code Playgroud) react-redux ×10
reactjs ×7
javascript ×5
redux ×3
next.js ×2
node.js ×2
react-native ×2
arrays ×1
gatsby ×1
react-hooks ×1
rxjs ×1