我正在使用React,semantic-ui-react,redux-subspace构建一个小应用程序。 我有许多不同的表,当用户点击其中一个单元格时,该值应该出现在控制台上但结果在单击时未定义。我正在尝试重用reducer。与不同实例的相同操作。 我感谢任何指导我正确指导的意见。
此组件呈现表格并用<SubspaceProvider>
包装。
<Segment inverted color='black'>
<h1>Age </h1>
{ this.state.toggle ?
<SubspaceProvider mapState={state => state.withSpouseAge} namespace="withSpouseAge">
<TableForm
headers={spouse_ageHeaders}
rows={spouse_ageData}
namespace={'withSpouseAge'}
/>
</SubspaceProvider> :
<SubspaceProvider mapState={state => state.withoutSpouseAge} namespace="withoutSpouseAge">
<TableForm
headers={withoutSpouse_ageHeader}
rows={withoutSpouse_ageData}
namespace={'withoutSpouseAge'}
/>
</SubspaceProvider> }
此组件返回带有数据的表,这是我想要实现onClick方法的地方。
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Table } from 'semantic-ui-react';
import { select } from '../actions';
const shortid = require('shortid');
class TableForm extends Component {
constructor(props){
super(props);
this.state = {
activeIndex: 0,
}
this.handleOnClick = this.handleOnClick.bind(this);
this.isCellActive = this.isCellActive.bind(this);
};
isCellActive(index) {
this.setState({ activeIndex: index });
}
handleOnClick(index, point) {
this.isCellActive(index);
this.props.onSelect(point);
};
tableForm = ({ headers, rows }) => {
const customRenderRow = ({ factor, point, point2 }, index ) => ({
key: shortid.generate(),
cells: [
<Table.Cell content={factor || 'N/A'} />,
<Table.Cell
content={point}
active={index === this.state.activeIndex}
textAlign={'center'}
selectable
onClick={() => this.handleOnClick(index, point)}
/>,
<Table.Cell
content={point2}
textAlign={'center'}
selectable
/>
],
});
return (
<Table
size='large'
padded
striped
celled
verticalAlign={'middle'}
headerRow={this.props.headers}
renderBodyRow={customRenderRow}
tableData={this.props.rows}
/>
)
};
render() {
console.log(this.props.withSpouseAgePoint);
const { headers, rows } = this.props;
return (
<div>
{this.tableForm(headers, rows)}
</div>
);
}
};
const mapDispatchToProps = (dispatch) => {
return {
onSelect: (point) => {dispatch(select(point))},
}
}
const mapStateToProps = state => {
return {
withSpouseAgePoint: state.withSpouseAge,
withSpouseLoePoint: state.withSpouseLoe,
}
}
export default connect(mapStateToProps, mapDispatchToProps)(TableForm);
import {
SELECT,
} from './types';
export const select = (points) => ({
type: 'SELECT',
points,
});
import { SELECT } from '../actions/types';
const INITIAL_STATE = {
point: 0,
};
const selectionReducer = (state = INITIAL_STATE, action) => {
switch (action.type) {
case 'SELECT':
return { ...state, point: state.point + action.points };
default:
return state;
}
};
export default selectionReducer;
import { createStore, combineReducers } from 'redux';
import { subspace, namespaced } from 'redux-subspace';
import selectionReducer from './selectionReducer';
import toggleReducer from './toggleReducer';
const reducers = combineReducers({
withSpouseAge: namespaced('withSpouseAge')(selectionReducer),
withSpouseLoe: namespaced('withSpouseLoe')(selectionReducer),
withSpouseOlp: namespaced('withSpouseOlp')(selectionReducer),
withSpouseOlp2: namespaced('withSpouseOlp2')(selectionReducer),
withSpouseExp: namespaced('withSpouseExp')(selectionReducer),
withoutSpouseAge: namespaced('withoutSpouseAge')(selectionReducer),
withoutSpouseLoe: namespaced('withoutSpouseLoe')(selectionReducer),
withoutSpouseOlp: namespaced('withoutSpouseOlp')(selectionReducer),
withoutSpouseOlp2: namespaced('withoutSpouseOlp2')(selectionReducer),
withoutSpouseExp: namespaced('withoutSpouseExp')(selectionReducer),
toggle: toggleReducer,
});
我在TableForm组件下面添加了
const mapDispatchToProps = (dispatch) => {
return {
onSelect: (point) => {dispatch(select(point))},
}
}
const mapStateToProps = state => {
return {
withSpouseAgePoint: state.withSpouseAge,
withSpouseLoePoint: state.withSpouseLoe,
}
}
export default connect(mapStateToProps, mapDispatchToProps)(TableForm);
在handleOnClick上实现this.props.onSelect(point)
。它仍然显示了相同的结果undefined
。我按getState()
检查了商店状态。 consloe.log。我认为我对redux-subspace的实现是错误的。我上传了整个TableForm组件并更新了reducer。请帮帮我!
我替换了mapStateToProps
,它就像魔术一样。再次感谢@JustinTRoss。
但还有另一个问题,当我点击单元格时,所有状态都会出现相同的值。
。我的计划是每个州都有自己的值存储。
const mapStateToProps = state => {
return {
withSpouseAgePoint: state,
withoutSpouseAge: state,
}
}
答案 0 :(得分:2)
您已将组件命名为withSpouseAge,并在SubspaceProvider中将状态映射到state.withSpouseAge。因此,您调用的是state.withSpouseAge.withSpouseAge(undefined)。
另一个潜在的问题是您使用connect连接的签名。从您提供的代码段中,无法确定“选择”的值。通常,使用2个函数调用connect,通常命名为mapStateToProps和mapDispatchToProps。您正在使用函数和对象调用connect。以下是http://www.sohamkamani.com/blog/2017/03/31/react-redux-connect-explained/#connect的示例:
[innerHTML]="myText"
此外,还有另外一个问题,虽然它还没有影响到你:你使用2个参数(标题和行)调用this.tableForm,而你定义了this.tableForm函数来获取单个参数和destructure “标题”和“行”属性。