javascript - 向输入传递一个存储在 redux 中的值并使其可编辑
问题描述
我有一个输入,我使用存储在 redux 中其他地方的值预先填充了该值,该值state.model.operationLength
被传递给输入,因为this.props.operationLength.value
用户然后在输入中看到下面的输入,但是该值是不可编辑的,如果我尝试更改它,它会触发 redux 中的操作但不会更改值,
如果没有值,state.model.operationLength
则输入是可编辑的,并将存储输入的值
如果已经存储了一个值,我怎样才能使它成为可编辑的值?
组件.js
<TextBox
type="number"
defaultValue={ this.props.height / 100 * 66 }
placeholder={this.props.height / 100 * 66}
onChange={newValue => this.props.setOperationLength(newValue)}
tooltip="invalid"
value={this.props.operationLength.value || ''}
isValid={this.props.setOperationLengthValid}
state={this.props.operationLength.isValid}
/>
文本框.js
class TextBox extends React.Component {
onChange = event => {
const value = event.target.value;
value >= this.props.placeholder && value <= 3000
? this.props.isValid(true)
: this.props.isValid(false);
this.props.onChange(value);
};
render() {
return (
<Box invalid={!this.props.isValid}>
<Label
rollo={this.props.designation === "rollo"}
pleating={this.props.designation === "pleating"}
>
{this.props.label}
</Label>
<span>
<Input
type={this.props.type && this.props.type}
onChange={this.onChange}
placeholder={this.props.placeholder}
value={this.props.value || ""}
/>
mm
<Tooltip>
{this.props.state === false &&
this.props.value !== null &&
this.props.value <= 3000 &&
"number entereded is too low"}
{this.props.state === false &&
this.props.value !== null &&
this.props.value >= this.props.placeholder &&
"number entereded is too high"}
enter a number between ({this.props.placeholder} and 3000)
</Tooltip>
</span>
</Box>
);
}
}
export default TextBox;
动作.js
export function setOperationLength(operationLength) {
return {
type: SET_OPERATION_LENGTH,
operationLength
}
}
减速器.js
import {
SET_OPERATION_LENGTH, SET_OPERATION_LENGTH_VALID, SET_OPERATION_PRISTINE
} from '../../constants/actionTypes.js';
const operationLengthReducer = (state = {
isValid: false,
pristine: true,
value: 0
}, action) => {
switch (action.type) {
case SET_OPERATION_LENGTH:
return {
...state,
value: action.operationLength
};
case SET_OPERATION_LENGTH_VALID:
return {
...state,
isValid: action.isValid
};
case SET_OPERATION_PRISTINE:
return {
...state,
pristine: action.pristine
};
default:
return state;
}
}
export default operationLengthReducer
解决方案
推荐阅读
- python - 使用 django 2 添加类别的 url
- reactjs - 如何使用 React Native 创建动态数量的组件?
- javascript - 如何使用 exceljs 从 .xls 文件中读取?
- amazon-web-services - 使用联合用户的权利表控制对 Amazon DynamoDB 条目的访问
- c# - 如何在 asp.net core 3 中设置 json 序列化程序设置?
- svn - 通过校验和(SHA Hash)获取存储到 Subversion(SVN)的文件
- java - Classname.Variable 与 Classname 作为变量一起使用
- bazel - 外部依赖项中的 .bzl 文件
- spring - Hibernate 似乎从 HQL 查询为 oracle 方言创建了错误的 SQL 查询
- c# - WPF ListView 通过按钮单击滚动整个页面(看到的项目)