【发布时间】:2019-12-09 11:20:35
【问题描述】:
所以我在 react proj 结构上使用了这个很棒的 react-context 和钩子,以便将值从状态传递到组件,但是当我解构时,孩子们警告我 children' is missing in props validationeslint(react/prop-types)。
问题是我真的不想只为那个 eslint 警告导入 PropTypes,所以在没有 PropTypes 的情况下最好的方法是什么。
import React, { useState, createContext } from "react";
export const FormContext = createContext();
const FormContextProvider = ({ children }) => {
const [state, setState] = useState({
firstName: "",
LastName: ""
});
const updateState = () => {
setState({
...state,
firstName: "Method",
LastName: "Man"
});
};
return (
<FormContext.Provider value={{ ...state, updateState }}>
{children}
</FormContext.Provider>
);
};
export default FormContextProvider;
【问题讨论】:
-
/* eslint-disable react/prop-types */ 如果 children 是它使用的唯一属性,您可以禁用此文件的 eslint 规则。如果组件在某个时候被扩展,您可能应该注意 eslint 不会验证这一点。
标签: javascript reactjs react-hooks react-context