因此,我使用了这个很棒的react-context并挂在react proj结构上,以便将状态中的值传递给component,但是当我分解子级时,警告我children' is missing in props validationeslint(react/prop-types)
。
事实是,我真的不是只想为该警告而导入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;
答案 0 :(得分:0)
您可以在没有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>
);
};