Reactjs 无法更新react中的父属性
我可以插入输入值,比如“1,2,3”,当退格时,除了控制台中的“1”之外,它会删除所有内容,即,Reactjs 无法更新react中的父属性,reactjs,react-native,react-redux,Reactjs,React Native,React Redux,我可以插入输入值,比如“1,2,3”,当退格时,除了控制台中的“1”之外,它会删除所有内容,即,House{props{House{property{rent:1}}}} 我在这里提供的代码有3个文件 (1)house.js import ValInput from "main/components/val-input"; class House extends Component { state = { rent:"", }; componentDidMount() {
House{props{House{property{rent:1}}}}
我在这里提供的代码有3个文件
(1)house.js
import ValInput from "main/components/val-input";
class House extends Component {
state = {
rent:"",
};
componentDidMount() {
if (this.props.house.rent) {
const { rent} = this.props.house;
this.setState({ rent });
}
}
onChange = (e) => {
const rent = parseInt(e.target.value.replace(string);
this.setState({
rent,
});
};
render(){
const {house} = this.props;
const {rent} = this.state;
...
<ValInput
type="text"
value={ rent }
onChange={e => {
this.onChange(e);
}}
/>
}
import React from "react";
import Input from "main/components/input";
const ValInput = props => (
<Input
{...props}
type={props.type ? props.type : "text"}
/>
);
export default valInput;
import React from "react";
const noOp = () => {};
const Input = ({
onBlur = xP,
...otherProps
}) => (
<input
onBlur={e => {
e.target.placeholder = placeholder;
onBlur(e);
}}
{...otherProps}
/>
);
export default Input;
(3)组件/input/index.js
import ValInput from "main/components/val-input";
class House extends Component {
state = {
rent:"",
};
componentDidMount() {
if (this.props.house.rent) {
const { rent} = this.props.house;
this.setState({ rent });
}
}
onChange = (e) => {
const rent = parseInt(e.target.value.replace(string);
this.setState({
rent,
});
};
render(){
const {house} = this.props;
const {rent} = this.state;
...
<ValInput
type="text"
value={ rent }
onChange={e => {
this.onChange(e);
}}
/>
}
import React from "react";
import Input from "main/components/input";
const ValInput = props => (
<Input
{...props}
type={props.type ? props.type : "text"}
/>
);
export default valInput;
import React from "react";
const noOp = () => {};
const Input = ({
onBlur = xP,
...otherProps
}) => (
<input
onBlur={e => {
e.target.placeholder = placeholder;
onBlur(e);
}}
{...otherProps}
/>
);
export default Input;
从“React”导入React;
常量noOp=()=>{};
常量输入=({
onBlur=xP,
…其他道具
}) => (
{
e、 target.placeholder=占位符;
安布尔(e);
}}
{…其他道具}
/>
);
导出默认输入;
预期的结果应该是,在清空值(比如使用backspace)后,下次访问页面时,输入字段应该是空的,并且不应该显示旧值 检查此代码沙盒我复制了您的代码,如果我正确理解了问题,请修复它
更新@NaderZouaoui时,给了我一个如何回拨的示例:
1.子文件:
onChange={e=>{
本条第1款(e)项;
}}
onChange=e=>{
这是我的国家({
租金
});
本.道具.回拨(租金);
};
2.父文件:
状态={
租金:“
};
handleChangeRent=新租金=>{
this.setState({rent:newRent});
};
render(){
返回(
);
}
请检查onChange
它似乎不正确(或者您可能忘记了一些代码?)谢谢,它是正确的…我没有添加正则表达式…但显示的代码有限..您可以为相同的内容添加一个plunker或fiddle吗您似乎在任何地方都没有使用。tbh没有足够的项目特有的正确信息来解决问题。您好,ValInput用于在输入字段中输入值,感谢我将其保留为现在编辑的状态,其余部分是正确的。哇,您最初的想法和通过CodeSandbox提供的帮助非常有帮助,实际上,您发送给我的用于执行回调的工作确实有效,但根据我的工作,我需要在查询中进行更改并在缓存中更新它。。!但你的回电很棒。非常感谢!