javascript - 如何声明 proptypes 以验证特定字符串的数组?
问题描述
传递给组件的值可以是字符串数组。该字符串表示不同的方向:top
, right
, bottom
, left
.
如何为组件指定 proptypes 以仅允许指定方向的数组?
<Arrows
directions=['left', 'bottom'] // accepts the array
/>
<Arrows
directions=['left', 'back'] // doesn't accept the array
/>
解决方案
像这样的东西应该可以完成这项工作:
directions: PropTypes.arrayOf(PropTypes.oneOf(['top', 'right', 'bottom', 'left'])),
推荐阅读
- c++ - 预先声明子类
- python - conftest.py 可以读取 pytest.ini 中的现有值吗?
- ruby-on-rails - ActionController::BadRequest(无效的请求参数:ajax 帖子上的预期数组(获取 Rack::QueryParser::Params)
- mysql - 用于添加一些数据的 SQL 触发器
- python - 不支持未知 - f1 分数
- javascript - 如何在 React Hooks 中切换复选框值?
- java - 单击后防止 JButton repaint()
- c - 将多个作为文件名的参数从多个 txt 文件传递给在 linux 终端中运行的 ac 程序
- oracle - 无法为 oracle 表中的更改日志编译触发器
- node.js - Nodemailer“连接ETIMEDOUT”与自定义域