[英]How to use props to update my form Reactjs
I have an Update Dialog Component which needs to update some values.我有一个更新对话框组件,它需要更新一些值。 My update dialog looks like following.
我的更新对话框如下所示。
class EditWebsiteComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
};
}
handleChange = name => event => {
let temp = this.props.selectedWebsite;
temp[name] = event.target.value;
console.log(temp);
this.props.changesSelectedWebsite(temp);
// this.setState({
// [name]: event.target.value,
// });
};
onFileChange = event => {
this.setState({ logo: event.target.files[0] });
};
render() {
const openDialog = this.props.openDialog;
const {selectedWebsite} = this.props;
return (
<div>
{/*{this.props.selectedWebsite && this.props.selectedWebsite.title}*/}
<Dialog
fullWidth={true}
open={openDialog}
onClose={this.props.closeDialog}
>
<DialogTitle>
{"Edit Website"}
</DialogTitle>
<DialogContent>
<FormControl className="w-100">
<TextField
style={{'margin-right': "10px"}}
className="col-md-11 col-11"
id="name"
label="Title"
value={selectedWebsite.title}
onChange={this.handleChange('title')}
margin="normal"
fullWidth
/>
<TextField
style={{'margin-right': "10px"}}
className="col-md-11 col-11"
id="name"
label="URL"
value={selectedWebsite.url}
onChange={this.handleChange('url')}
margin="normal"
fullWidth
/>
<div style={{"margin-top": "20px"}} className='col-md-11 col-11 flex-class-custom'>
<input type="file" onChange={this.onFileChange} />
</div>
</FormControl>
</DialogContent>
<DialogActions>
<Button onClick={this.props.closeDialog} color="secondary">
Close
</Button>
<Button onClick={() =>
this.props.editWebsite({title: selectedWebsite.title, url:selectedWebsite.url, logo:selectedWebsite.logo, id:selectedWebsite.id})
} color="primary">
Edit
</Button>
</DialogActions>
</Dialog>
{this.props.showMessage && NotificationManager.error(this.props.alertMessage)}
<NotificationContainer/>
</div>
)
}
}
const mapDispatchToProps = dispatch => ({
editWebsite: (payload) => dispatch(editWebsite(payload)),
changesSelectedWebsite: (payload) => dispatch(changesSelectedWebsite(payload))
});
const mapStateToProps = state => ({
selectedWebsite : state.Websites.selectedWebsite,
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(EditWebsiteComponent)
I am getting the current values from selected website and on change i am updating the props by dispatching the changesSelectedWebsite action, but the dialog form is not being updated, although props are being updated.我正在从选定的网站获取当前值,并且在更改时我正在通过调度 changesSelectedWebsite 操作来更新道具,但对话框表单没有被更新,尽管道具正在更新。 I am quite new to react and redux so i wanted to ask if this approach is right?
我对反应和 redux 很陌生,所以我想问一下这种方法是否正确? and how else can i achieve this because i need values of selectedWebsites and also need to update if user changes anything.
以及我还能如何实现这一点,因为我需要 selectedWebsites 的值,并且如果用户更改任何内容也需要更新。
Can you try changing this line你能试着改变这条线吗
this.props.changesSelectedWebsite(temp);
to至
this.props.changesSelectedWebsite({...temp});
As long as you are getting the prop value as is without "recreating it", the state wont feel the change, so, {...temp} recreates the object, why?只要您在没有“重新创建”的情况下按原样获得道具值,state 就不会感觉到变化,所以,{...temp} 重新创建了 object,为什么? because it tells the state that this is a new object and triggers a rerender and saves the new state.
因为它告诉 state 这是一个新的 object 并触发重新渲染并保存新的 state。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.