gpt4 book ai didi

javascript - 以 Redux 形式动态加载 initialValues

转载 作者:数据小太阳 更新时间:2023-10-29 03:48:28 24 4
gpt4 key购买 nike

initializingFromState为例在 Redux-Form 中,我试图动态设置它。这是编辑书籍列表中的特定书籍,并使用在 express.js 中设置的简单 api。

完整容器如下。我不知何故需要在 mapStateToProps 函数中传递 initialValues。在示例中,它是通过静态对象完成的,但我不知道如何使用通过 fetchBook 获取的信息,并将其传递给 initialValues .

容器:

import React, { Component, PropTypes } from 'react';
import { reduxForm } from 'redux-form';
import { connect } from 'react-redux';
import { Link } from 'react-router';
import { fetchBook, editBook } from '../actions/index';

class BookEdit extends Component {

componentWillMount() {
this.props.fetchBook(this.props.params.id);
}

static contextTypes = {
router: PropTypes.object
}

onSubmit(props) {
this.props.editBook(this.props.book.id, props)
.then(() => {
this.context.router.push('/');
});
}

const data = {
title: {this.props.book.title},
description: {this.props.author}
}

render() {

const { fields: { title, author }, handleSubmit } = this.props;
const { book } = this.props;

if (!book) {
return (
<div>
<p>Loading...</p>
</div>
)
}

return (
<div>
<Link to="/">Back</Link>
<form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
<h2>Add a new book</h2>

<label>Title</label>
<input type="text" {...title} />
<div className="text-help">{title.touched ? title.error : ''}</div>

<label>Author</label>
<input type="text" {...author} />
<div className="text-help">{author.touched ? author.error : ''}</div>

<button type="submit">Add</button>
<Link to="/" className="button">Go back</Link>
</form>
</div>
);
}
}

function mapStateToProps(state) {
return {
book: state.books.book,
initialValues: // how do I pass in the books here?
};
}

export default reduxForm({
form: 'EditBookForm',
fields: ['title', 'author']
}, mapStateToProps, { fetchBook, editBook })(BookEdit);

谢谢。

最佳答案

您的表单值不是 state.books.book 中的值?我想这就是您要找的:

function mapStateToProps(state) {
return {
book: state.books.book,
initialValues: state.books.book
};
}

由于您实际上只是在查看 this.props.book 以了解它是否已加载,因此执行以下操作可能更明确:

function mapStateToProps(state) {
return {
loaded: !!state.books.book,
initialValues: state.books.book
};
}

希望对您有所帮助。

关于javascript - 以 Redux 形式动态加载 initialValues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37116733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com