所以我在一个react项目结构上使用了这个很棒的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;发布于 2019-12-09 19:36:32
您可以在不导入PropTypes的情况下尝试以下操作
const FormContextProvider = (props) => {
const [state, setState] = useState({
firstName: "",
LastName: ""
});
const updateState = () => {
setState({
...state,
firstName: "Method",
LastName: "Man"
});
};
return (
<FormContext.Provider value={{ ...state, updateState }}>
{props['children']}
</FormContext.Provider>
);
};https://stackoverflow.com/questions/59247783
复制相似问题