【问题标题】:'children' is missing in props validation道具验证中缺少“孩子”
【发布时间】: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


【解决方案1】:

你可以在下面不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>
  );
};

【讨论】:

    猜你喜欢
    • 2021-09-04
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2017-09-26
    • 2020-10-29
    • 2018-04-02
    相关资源
    最近更新 更多