[英]How to clean the state on screen change ?(using conditional rendering)
What I'm trying to do is clean a state of my objects props in my class component on screen change我要做的是在屏幕更改时清理我的 class 组件中的对象道具的 state
here is where I'm rendering the fields:这是我渲染字段的地方:
class InputBody extends Component {
constructor(props) {
super(props);
this.state = {
fields: JSON.parse(this.props.route).message,
};
}
render() {
return (
<Fragment>
{Object.keys(JSON.parse(this.props.route).message).length > 0 ? (
<FieldArraysForm all={JSON.parse(this.props.route).message} native={this.props} />
) : (
<ActivityIndicator size="large" color="#eb6b09" />
)}
</Fragment>
);
}
}
Then I'm handling my fields and input types in here:然后我在这里处理我的字段和输入类型:
class RenderField extends Component {
render() {
return (
<Fragment>
<Texto>{this.props.label}</Texto>
<TextInput
onChangeText={this.props.input.onChange}
{...this.props.input}
keyboardType={this.props.type}
/>
</Fragment>
);
}
}
And finally I'm getting the values:最后我得到了这些值:
class FieldArraysForm extends Component {
render() {
const {handleSubmit} = this.props.native;
// event listener
const getFields = async (values) => {
return sleep(500).then(() => {
// implementar prop.reset() as soon as I leave the screen, but how can I make it ?
console.log(JSON.stringify(values))
})
}
return (
<Form>
{this.props.all.map((item) => (
<Field
key={item._id}
name={`customInput.${item._id}`}
component={RenderField}
label={item.field}
type={item.typeFieldAltText}
/>
))}
<View>
<TouchableOpacity onPress={handleSubmit(getFields)}>
<Text>Save Form</Text>
</TouchableOpacity>
</View>
</Form>
);
}
}
Someone knows how can I clean my state in class component
?有人知道如何在
class component
中清理我的 state 吗?
Obs: I'm using: react-navigation v6
. Obs:我正在使用:
react-navigation v6
。
Make a reset function in your InputBody component and pass that function as a prop to your FieldArrayForm compoent.在 InputBody 组件中重置 function 并将 function 作为道具传递给您的 FieldArrayForm 组件。
class InputBody extends Component {
constructor(props) {
super(props);
this.state = {
fields: JSON.parse(this.props.route).message,
};
}
reset () {
this.setState({});
}
render() {
return (
<Fragment>
{Object.keys(JSON.parse(this.props.route).message).length > 0 ? (
<FieldArraysForm all={JSON.parse(this.props.route).message} resetForm={reset} native={this.props} />
) : (
<ActivityIndicator size="large" color="#eb6b09" />
)}
</Fragment>
);
}
}
Then call that reset props in your FieldArraysForm component然后在您的 FieldArraysForm 组件中调用该重置道具
const getFields = async (values) => {
return sleep(500).then(() => {
// implementar prop.reset() as soon as I leave the screen, but how can I make it ?
console.log(JSON.stringify(values))
this.props.reset()
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.