我使用带有钩子的 React 上下文作为我的 React 应用程序的状态管理器。每次 store 中的值发生变化时,所有组件都会重新渲染。
有没有办法阻止 React 组件重新渲染?
店铺配置:
import React, { useReducer } from "react";
import rootReducer from "./reducers/rootReducer";
export const ApiContext = React.createContext();
export const Provider = ({ children }) => {
const [state, dispatch] = useReducer(rootReducer, {});
return (
<ApiContext.Provider value={{ ...state, dispatch }}>
{children}
</ApiContext.Provider>
);
};
Run Code Online (Sandbox Code Playgroud)
减速器的一个例子:
import * as types from "./../actionTypes";
const initialState = {
fetchedBooks: null
};
const bookReducer = (state = initialState, action) => {
switch (action.type) {
case …
Run Code Online (Sandbox Code Playgroud) 我在我的项目(React、TS、Mobx)中添加了 onChange 方法,但出现错误: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type
我是 TypeScript 的新手,不知道为什么会这样。可能是什么问题?
(parameter) event: {
target: {
name: any;
value: any;
};
}
Run Code Online (Sandbox Code Playgroud)
'{ [x: number]: any; 类型的参数 }' 不可分配给类型为 'IAddPlayerFormState | 的参数 ((prevState: Readonly, props: Readonly) => IAddPlayerFormState | Pick | null) | 选择<...> | 空值'。
import React, { Component } from 'react';
import ViewStore from '../../../store/ViewStore';
import { TextField } from './../../../utils';
interface IAddPlayerFormProps {
viewStore?: ViewStore; // Optional …
Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用 react-navigation,我需要在右标题按钮单击时切换模式。当我 setState 为 true 时,我得到未定义。
代码:
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { ToggleDrawer, ToggleAdditional } from '../../utils';
import { AdditionalModal } from '../../components';
class Settings extends Component {
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
headerTitle: 'Profile',
headerLeft: <ToggleDrawer pressHandler={() => navigation.openDrawer()} />,
headerRight: (
<ToggleAdditional pressHandler={() => params.handleAdditionalModal()} />
)
};
};
constructor(props) {
super(props);
this.state = …
Run Code Online (Sandbox Code Playgroud) 对于这段代码,我收到 eslint(AirBnb config) 错误:引用前一个状态时在 setState 中使用回调
此错误是否会以某种方式影响性能以及如何解决?
handleSelect(event) {
const entry = event.nativeEvent;
if (entry == null) {
this.setState({ ...this.state, selectedEntry: entry });
} else
this.setState({
...this.state,
selectedEntry: JSON.stringify(entry),
markerIsEnabled: true
});
console.log(event.nativeEvent);
}
Run Code Online (Sandbox Code Playgroud) 我在React Native应用程序中使用Formik。在登录表单上,我有两个字段:电子邮件和密码,这两个字段都是必需的。
我已经写了这样的验证规则:
export const LoginSchema = Yup.object().shape({
email: Yup.string()
.email('The email is invalid')
.required('This field is required'),
password: Yup.string()
.min(6, 'The password is too short')
.max(12, 'The password is too long')
.required('This field is required'),
});
Run Code Online (Sandbox Code Playgroud)
我只需要在表单提交时触发验证并显示错误弹出窗口。我已经阅读了文档,但是找不到解决方案,因为验证会触发onBlur。如何才能做到这一点?
谢谢!
const Login = ({ navigation }) => {
const [isLoading, setIsLoading] = useState(true);
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
// Later check for token
const tokenIsStored = true;
if (tokenIsStored) {
setIsLoading(false);
}
});
const onLogin = values => …
Run Code Online (Sandbox Code Playgroud) 我正在使用这样的命令来生成一个 APK:
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res && react-native run-android
Run Code Online (Sandbox Code Playgroud)
是否可以编辑此命令以生成发布版 APK?
我正在使用 React Hooks,当我用来自 mobx 的观察者包装我的组件时,我收到了这个错误。可能是什么问题?是否可以将 mobx 与 React Hooks 一起使用?
import classnames from 'classnames';
import { observer } from 'mobx-react';
import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import ViewStore from '../../../store/ViewStore';
interface INavbarProps {
viewStore: ViewStore;
}
const Navbar = observer((props: INavbarProps) => {
const { authed } = props.viewStore;
const [drawerIsOpen, setState] = useState(false);
function toggleMenu() {
drawerIsOpen ? setState(false) : setState(true);
}
return (
<div>
<Link to="/">Home</Link>
<Link to="/admin">Admin</Link>
<Link to="/all">All</Link>
{authed …
Run Code Online (Sandbox Code Playgroud) 我已经使用 Microsoft App Center 设置了 ci/cd。它与调试 APK 完美配合,但在构建发布 APK 时出现错误。证书已上传,应用程序中的所有内容均已正确设置。没有这样的问题,当我尝试构建它时,使用 npm 命令:
"android-release": "cd android && ./gradlew assembleRelease"
可能是什么问题?
> Task :app:validateSigningRelease FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:validateSigningRelease'.
>
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.2/userguide/command_line_interface.html#sec:command_line_warnings
Keystore file '/Users/vsts/agent/2.150.0/work/1/s/android/app/my-release-key.keystore' not found for signing config 'release'.
* Try:
Run with --stacktrace option to …
Run Code Online (Sandbox Code Playgroud) 我有来自服务器的 ISO 格式的日期:2019-02-28。如何使用 Moment.js 添加 7 天?
结果应该是2019-03-04。
react-native ×6
reactjs ×6
javascript ×5
android ×2
mobx ×2
typescript ×2
crashlytics ×1
eslint ×1
firebase ×1
formik ×1
gradle ×1
ios ×1
momentjs ×1
node.js ×1
npm ×1
react-hooks ×1
validation ×1
vue-cli ×1
vue.js ×1
yup ×1