我正在尝试用redux学习反应js。
这是我的View.js
export default HomeView
//Import React
import React from 'react'
//Import Redux Components
import { connect } from 'react-redux';
//Import Action
import { addAcct, upsertAccts, qryAccts, updateAccts } from '../../Counter/modules/counter';
class HomeView extends React.Component {
constructor (props)
{
super(props);
}
componentWillMount()
{
this.props.dispatch(qryAccts());
console.log(this.props);
this.forceUpdate();
}
componentDidMount()
{
console.log('----Did----',this.props.accts);
}
//Perform Upsert Actions
upsertAccts(e)
{
this.props.dispatch(upsertAccts(this.props.accts))
}
//Add new Account to the Array
addAcct(event)
{
this.props.dispatch(addAcct());
console.log('===this.props===',this.props);
}
//onChange function to handle when a name is changed
handleChange(ev,index)
{
this.props.dispatch(updateAccts(ev.target.value,index));
}
dateChange(e)
{
console.log(e.target.value);
}
render() {
console.log('-----this.props.accts-------',this.props.accts);
return (
<div>
<h1>Accounts</h1>
<ul>
{ this.props.accts.map((v,i) => <li key={i}><input style={{width:'500px'}} onChange={event => this.handleChange(event,i)} value={v.Name}/></li>) }
</ul>
</div>
);
}
}
//Connects Redux State to React, Injects reducer as a property
//export default Page1Demo;
export default connect(state => ({ accts: state.accts }))(HomeView);
我的减速机动作在这里
// ------------------------------------
// Constants
// ------------------------------------
export const RECEIVED_ACCTS = 'RECEIVED_ACCTS';
export const UPSERT_ACCTS = 'UPSERT_ACCTS';
export const ADD_ACCT = 'ADD_ACCT';
export const UPDATE_ACCTS = 'UPDATE_ACCTS';
// ------------------------------------
// Actions
// ------------------------------------
export function recAccts(accts) {
console.log('------16----',accts);
return{
type: RECEIVED_ACCTS,
accts: accts
}
}
export function qryAccts()
{
return dispatch => {
ResponsiveCtrl.getAccts(function(r, e) {
console.log('------26----',r);
console.log('------26----',e);
dispatch(recAccts(r))
},{escape:false});
}
}
export function upsertAccts(acctStore) {
return dispatch => {
ResponsiveCtrl.upsertAccts(acctStore, function(r, e) {
dispatch(recAccts(r))
},{escape:false});
}
}
export function addAcct() {
return {
type: ADD_ACCT
}
}
export function updateAccts(name,index) {
return {
type: UPDATE_ACCTS,
acctName:name,
listIndex:index
}
}
/* This is a thunk, meaning it is a function that immediately
returns a function for lazy evaluation. It is incredibly useful for
creating async actions, especially when combined with redux-thunk!
NOTE: This is solely for demonstration purposes. In a real application,
you'd probably want to dispatch an action of COUNTER_DOUBLE and let the
reducer take care of this logic. */
// ------------------------------------
// Action Handlers
// ------------------------------------
// ------------------------------------
// Reducer
// ------------------------------------
const initialState = [];
//Reducer function
export function accts(state = initialState, action) {
console.log('==action===',action);
console.log('==initialState===',initialState);
console.log('==state===',state);
switch (action.type) {
case RECEIVED_ACCTS:
//Return the Accouts we receive from Salesforce
state = action.accts;
return state;
case UPDATE_ACCTS:
//Update our array at the specific index
var newState = state.slice();
newState[action.listIndex].Name = action.acctName;
return newState;
case ADD_ACCT:
//Add a new Account to our Array
var newState = state.slice();
newState.push({Name:""});
return newState;
default:
return state
}
}
我能够确定问题但不确定如何解决问题。
问题是dom首先生成,我后来得到的值是为什么我的地图最初是空的并且它给了我错误
TypeError:无法读取属性&#39; map&#39;未定义的
知道如何解决此问题。
答案 0 :(得分:0)
使用this.props.accts
上的验证将该代码分解为自己的方法。
...
renderAccts () {
if (Array.isArray(this.props.accts)) {
return this.props.accts.map((v,i) => <li key={i}><input style={{width:'500px'}} onChange={event => this.handleChange(event,i)} value={v.Name}/></li>)
}
return ''
},
render () {
...
<div>
<h1>Accounts</h1>
<ul>
{this.renderAccts()}
</ul>
</div>
}
这是一个很好的做法,因为你想要确保你得到了你对props
的期望,但如果事实证明你的初始假设不是,那么它也会让你进一步调试问题的原因。您现在可以在地图前轻松添加console.log(this.props.accts)
,看看发生了什么。每次收到新的道具(触发渲染)时都会调用此方法。如果在第一次调用render时未定义该prop,则在尝试映射时会出现此错误。如果问题仍然存在,即使进行了此验证,您也应确保实际获得预期的道具。