如何将道具从一个组件传递到另一个组件以及道具验证 - react钩子
嗨,我正在学习道具验证。我有两个功能 app 和 Person 。从人函数我将变量传递到应用程序。变量是(名称,首选,年龄)。我需要按以下
- 名称应用验证:应该是一个
- 优先的城市:数组类型
- 年龄:应该是数字,应大于18且小于60 对于上述变量。
在执行此应用程序时,如果验证失败,我需要传递默认值(名称:“史蒂夫”,preferrediccities:['chennai','blr'],年龄:18)。我为上述问题写了逻辑,但是当我尝试根据条件使用三元操作来渲染输出时,我的逻辑无法正常工作。任何人都可以指导我在哪里犯错。提前致谢。
const Person = (props) => {
return (
<ul>
<li>
{props.strName === props.strName ? (
props.strName
) : (
<p>{Person.defaultProps.Name}</p>
)}
</li>
<li>{props.num}</li>
<li> {props.arr}</li>
</ul>
);
};
Person.propTypes = {
strName: PropTypes.string,
num: PropTypes.number,
arr: PropTypes.array.isRequired,
};
Person.defaultProps = {
Name: "Steve",
preferredCities: ["Chennai", "Bangalore"],
age: 18,
};
function App() {
return (
<div className="App">
<Person strName={18} num={18} arr={["A", "B"]} />
</div>
);
}
Hi I am learning props validation. I am having two functions APP and Person. From the Person function I am passing variables to the APP. The variables are(Name, preferredCities, Age). I need to apply validations as follows
- Name: Should be a string
- Prefered Cities: Array type
- Age: Should be number and should be greater than 18 and less than 60
for above passed variables.
While executing this app if validation fails I need to pass default values(Name:"Steve", preferredCities:['Chennai','BLR'],Age:18). I wrote the logic for the above problem but when I am trying to render the output based on the condition using ternary operation my logic is not working. Any one can guide me where I am making mistake. Thanks in advance.
const Person = (props) => {
return (
<ul>
<li>
{props.strName === props.strName ? (
props.strName
) : (
<p>{Person.defaultProps.Name}</p>
)}
</li>
<li>{props.num}</li>
<li> {props.arr}</li>
</ul>
);
};
Person.propTypes = {
strName: PropTypes.string,
num: PropTypes.number,
arr: PropTypes.array.isRequired,
};
Person.defaultProps = {
Name: "Steve",
preferredCities: ["Chennai", "Bangalore"],
age: 18,
};
function App() {
return (
<div className="App">
<Person strName={18} num={18} arr={["A", "B"]} />
</div>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为您需要将其中一个props.strname更改为propstypes.strname。
尝试此
{props.strname === proptypes.strname? (
..剩余的代码,我认为这是通过道具的更好方法。
I think you need to change one of the props.strName to propsTypes.strName.
try this
{props.strName === propTypes.strName ? (
.. remaining codesAnd I think this is a better way to pass props.
尝试这个男人:
Try this man :
默认道具不是在支撑验证失败时被采取的。默认道具是当在其母体组件中调用道具时未传递该道具时的后备值。
字符串和数组验证是用于
数字的用例的正常验证,您可以具有一些自定义验证:
那么您可以进行验证,例如:
但是,如果验证失败,则不会指定默认值。这只是编译时类型的检查器。
Default props are not for being taken when prop validation fails. Default props are fallback values when the prop is not passed when it is called in its parent component.
The string and the array validation are normal validations for your use case
For the number, you can have some custom validations:
Then you could have your validation like:
However, this does not take the default values specified if the validation fails. This is just a type checker at compile time.
尝试以下操作:
try this :
首先,这是Starname的拼写错误,应该是人组件中的 strname 。
First of all, it's a spelling mistake of starName should be
strName
in the person component.您可以将道具验证为人组成部分。只需检查道具值并有条件使用该值即可。如果需要,请尝试以下代码。
You can validate your props into the Person component. Just check the props value and conditionally use the value. If you want please try the below-given code.