首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >属性验证中缺少“props”

属性验证中缺少“props”
EN

Stack Overflow用户
提问于 2019-12-09 19:20:35
回答 1查看 2.4K关注 0票数 0

所以我在一个react项目结构上使用了这个很棒的react-context和钩子,以便将值从状态传递到组件,但是当我解构时,子节点警告我children' is missing in props validationeslint(react/prop-types)。问题是,我并不是真的想只为了那个eslint警告而导入PropTypes,那么在没有PropTypes的情况下,最好的方法是什么呢?

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

发布于 2019-12-09 19:36:32

您可以在不导入PropTypes的情况下尝试以下操作

代码语言:javascript
复制
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>
  );
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59247783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档