reactjs - Reactjs 在 props 数组中设置状态
问题描述
我是 React.js 的新手,正在尝试构建表单验证。
有 Btn 组件。
const MyBtn = ({opts}) => {
if(!opts) {
opts = {};
}
return (
<button {...opts}}>{opts.value}</button>
)
}
Home.js 是一个登录页面。我不会添加您可能不需要解决此问题的其他代码。
class Home extends Component {
state = {
formValid: false
}
regexCheck = {
disabled : {this.state.formValid},
value: "LogIn",
type: "submit"
}
render(){
return(
<MyBtn opts={this.regexCheck}/>
)
}
}
//output that I'm trying to get
<button type="submit" disabled={this.state.formValid}>LogIn</button>
我收到一个错误,我无法在道具中调用 {this.state.formValid}。因为这是一个保留字。(对不起我的英语不好。)我觉得我完全错了。
无论如何,我认为我可以为此使用 getDerivedStateFromProps 方法......?所以我在 Home.js 中添加了这个但没有用。
static getDerivedStateFromProps(nextProps, prevState){
if(prevState.formValid !== nextProps.formValid){
return { formValid: nextProps.formValid };
}
return null;
}
如您所见,我仍然对道具和状态感到困惑。啊,但我还是要继续做这个项目。请给我任何想法来解决这个问题。
解决方案
这是无效的语法:
disabled : {this.state.formValid},
你只是想要:
disabled : this.state.formValid,
推荐阅读
- python - 使用 DeltaE 色差测量图像中 ROI 的色差?
- react-native - ios 中的排毒测试失败 - 中止 detox.init() 执行
- java - 如何知道整数在另一个整数中的位置
- c++ - 优化光线追踪器中的 BVH 遍历
- kotlin - 在数据类中拥有构造函数是否有限制?(科特林)
- c# - AutoRest 为方法返回类型生成可为空的布尔值
- webrtc - 是否有监听点击“停止分享”按钮的事件?
- python - Jupyter notebook 未使用 python 3.7 打开 anaconda 3
- java - 我需要知道这个子程序是如何工作的
- javascript - 如何在 keyup 上使用新值删除输入文本的默认值?