[英]How can I use Object.assign() to create this state object how I want it
I'm using Redux to create a simple contacts application. 我正在使用Redux创建一个简单的联系人应用程序。 My state object looks like the following:
我的状态对象如下所示:
{
"contacts": [
{
"name": "Miguel Camilo",
"phone": "123456789"
},
{
"name": "Peter",
"phone": "883292300348"
},
{
"name": "Jessica",
"phone": "8743847638473"
},
{
"name": "Michael",
"phone": "0988765553"
}
],
"activeContact": {
"name": "Peter",
"phone": "883292300348"
}
}
I am creating an action to add a contact. 我正在创建一个添加联系人的操作。 I want to add a new object to the
contacts
array within the state containing the name and number that are coming from input fields on the page. 我想在一个状态中向
contacts
数组添加一个新对象,该状态包含来自页面输入字段的名称和编号。 I am using Object.assign()
to try and do this but I am getting an error bundle.js:22875 Uncaught TypeError: Cannot convert undefined or null to object
and I believe it is coming from my use of Object.assign()
in the reducer. 我正在使用
Object.assign()
尝试执行此操作,但是却收到错误bundle.js:22875 Uncaught TypeError: Cannot convert undefined or null to object
,我相信这是由于我在Object.assign()
中的使用减速器。 This is my reducer code: 这是我的减速器代码:
export default function (state = {}, action) {
switch (action.type) {
case 'ADD_CONTACT':
return Object.assign({}, state, {
contacts: state.concat([
{
name: action.payload.name,
phone: action.payload.phone
}
])
})
default:
return state;
}
}
So if action.payload.name
is "Jeff" and action.payload.phone
is "5647876578" I want the state returned to look like: 因此,如果
action.payload.name
是“ Jeff”,而action.payload.phone
是“ 5647876578”,我希望返回的状态如下所示:
{
"contacts": [
{
"name": "Miguel Camilo",
"phone": "123456789"
},
{
"name": "Peter",
"phone": "883292300348"
},
{
"name": "Jessica",
"phone": "8743847638473"
},
{
"name": "Michael",
"phone": "0988765553"
},
{
"name": "Jeff",
"phone": "5647876578"
}
],
"activeContact": {
"name": "Peter",
"phone": "883292300348"
}
}
I can't seem to find the issue in my use of Object.assign() but I'm pretty sure this is where my error is coming from. 我似乎在使用Object.assign()时找不到问题,但是我很确定这是我的错误出处。 Any help is appreciated!
任何帮助表示赞赏!
Edit: Logging state in the reducer returns the following: 编辑:reducer中的日志记录状态返回以下内容:
[
{
"name": "Miguel Camilo",
"phone": "123456789"
},
{
"name": "Peter",
"phone": "883292300348"
},
{
"name": "Jessica",
"phone": "8743847638473"
},
{
"name": "Michael",
"phone": "0988765553"
}
]
My ContactsList component: 我的ContactsList组件:
class ContactList extends Component {
renderList() {
return this.props.contacts.map((contact) => {
return (
<li
key={contact.phone}
onClick={() => this.props.selectContact(contact)}
className='list-group-item'>{contact.name}</li>
);
});
}
render() {
return (
<ul className = 'list-group col-sm-4'>
{this.renderList()}
</ul>
);
}
}
function mapStateToProps(state) {
return {
contacts: state.contacts
};
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ selectContact: selectContact }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(ContactList)
My ActiveContact
portion of the state is getting modified like this in it's reducer: 状态的我的
ActiveContact
部分正在其reducer中进行如下修改:
export default function (state = null, action) {
switch (action.type) {
case 'CONTACT_SELECTED':
return action.payload
}
return state;
}
The reason that you are getting this error is that your initial state has no contacts
array: 您收到此错误的原因是您的初始状态没有
contacts
数组:
export default function (state = {}, action) {
...
}
Try revising your state so that it initially has an empty contacts array like so: 尝试修改您的状态,以使其最初具有一个空的通讯录数组,如下所示:
export default function (state = { contacts : [] }, action) {
...
}
Also, consider revising your 'ADD_CONTACT'
action to use Array#concat
like so: 另外,考虑修改您的
'ADD_CONTACT'
操作以使用Array#concat
如下所示:
case 'ADD_CONTACT':
return Object.assign({}, state, {
contacts: state.contacts.concat([
{
name: action.payload.name,
phone: action.payload.phone
}
])
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.