【问题标题】:How to use props to update my form Reactjs如何使用道具更新我的表单 Reactjs
【发布时间】:2020-08-11 05:26:15
【问题描述】:

我有一个更新对话框组件,它需要更新一些值。我的更新对话框如下所示。

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)

我正在从选定的网站获取当前值,并且在更改时我正在通过调度 changesSelectedWebsite 操作来更新道具,但对话框表单没有被更新,尽管道具正在更新。我对 react 和 redux 很陌生,所以我想问一下这种方法是否正确?以及我还能如何实现这一点,因为我需要 selectedWebsites 的值,并且如果用户更改任何内容也需要更新。

【问题讨论】:

    标签: reactjs redux react-redux


    【解决方案1】:

    你能不能换行试试

    this.props.changesSelectedWebsite(temp);
    

    this.props.changesSelectedWebsite({...temp});
    

    只要你在没有“重新创建”的情况下按原样获取道具值,状态就不会感觉到变化,所以,{...temp} 重新创建对象,为什么?因为它告诉状态这是一个新对象并触发重新渲染并保存新状态。

    【讨论】:

    • 谢谢,它现在可以工作了。您能告诉我是否可以通过任何方式将 props.selectedWebsite 保存为组件状态吗?因为我需要更改状态以防使用不更新并关闭对话框?
    • 为此我建议您在 React 文档中查找 useEffect Cleanup,它在组件卸载时运行,在您的情况下是在对话框关闭时运行。
    猜你喜欢
    • 2015-05-17
    • 2019-06-02
    • 2019-11-18
    • 2019-04-20
    • 2021-08-31
    • 2017-05-25
    • 2019-01-28
    • 2017-06-28
    • 2020-08-11
    相关资源
    最近更新 更多