简体   繁体   中英

'children' is missing in props validation

so I am using this awesome react-context and hooks on an react proj structure in order to pass values from state into component but when I'm destructuring the children is warning me that children' is missing in props validationeslint(react/prop-types) . The thing is I not really want to import PropTypes only for that eslint warning so what shoud be the best way to do it whithout 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;

You can try below without PropTypes import

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM