道具验证中缺少“儿童”

时间:2019-12-09 11:20:35

标签: javascript reactjs react-hooks react-context

因此,我使用了这个很棒的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;

1 个答案:

答案 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>
  );
};