简体   繁体   English

道具验证反应/道具类型中缺少“文本”

[英]'text' is missing in props validation react/prop-types

as you may get from the title, passing props in react is not working.正如您可能从标题中了解到的那样,在反应中传递道具是行不通的。 And i don´t get why.我不明白为什么。

function Global(props) {
  return (
    <div>
      <ResponsiveAppBar />
      <Grid sx={{ mt: 7 }}>
        {props.text}
        <HorizontalLinearStepper />
      </Grid>
    </div>
  );
}
  return (
    <div className="App">
      <Card>
        <CvContext.Provider value={value}>
          <Global text="kk" />
        </CvContext.Provider>
      </Card>
    </div>
  );

Try using state.尝试使用状态。 Get the variables into state and load the component when the state value gets filled.将变量放入状态并在填充状态值时加载组件。

const [titleText, setTitleText] = useState(props.text || “”); const [titleText, setTitleText] = useState(props.text || “”);

then inside your return part use {titleText}然后在你的返回部分使用 {titleText}

I haven't tested this.我没有测试过这个。 Give it a try.试试看。

Try destructuring the props like this:尝试像这样解构道具:

function Global({text}) {
  return (
    <div>
      <ResponsiveAppBar />
      <Grid sx={{ mt: 7 }}>
        {text}
        <HorizontalLinearStepper />
      </Grid>
    </div>
  );
}

You can keep the rest the same.您可以保持其余部分不变。 Destructuring props is (usually) a better practice.解构道具(通常)是一种更好的做法。

Does it work if you do如果你这样做有用吗

import PropTypes from 'prop-types';


function Global(props) {
  return (
    <div>
      <ResponsiveAppBar />
      <Grid sx={{ mt: 7 }}>
        {props.text}
        <HorizontalLinearStepper />
      </Grid>
    </div>
  );
}

Global.propTypes = {
  text: PropTypes.string
}

But your code should work even without it, although you would be getting that warning.但是即使没有它,您的代码也应该可以工作,尽管您会收到该警告。

  • this correction work for me这个修正对我有用

    import PropTypes from "prop-types"; function Global(props) { Global.propTypes = { text: PropTypes.string, }; return ( <div> <ResponsiveAppBar /> <Grid sx={{ mt: 7 }}> {props.text} <HorizontalLinearStepper /> </Grid> </div> ); } export default Global;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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