React, semantic-ui-react, redux-subspace를 사용하여 작은 앱을 제작하고 있습니다. 여러 테이블이 있는데 사용자가 셀 중 하나를 클릭하면 콘솔에서 값을 가져 오지만 결과는 클릭 할 때 정의되지 않습니다. 감속기를 재사용하려고합니다. 다른 인스턴스와 동일한 작업. 올바른 방향으로 안내하는 의견을 보내 주시면 감사하겠습니다.redux-subspace를 사용하여 동일한 작업으로 감속기를 재사용하는 방법
PartA.js
컴포넌트는 표를 렌더링하고 <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> }
TableForm.js
데이터와이 구성 요소를 반환 테이블과 내가 온 클릭 메소드를 구현하고자하는 곳이다.
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,
});
Reducer.js
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,
});
업데이트하는 index.js
나는
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)
을 구현 TableForm 구성 요소 아래에 추가. 그것은 여전히 나에게 같은 결과를 보여줍니다 undefined
. 상점 상태를 getState()
으로 확인했습니다. consloe.log. 나는 redux-subspace 구현이 잘못되었다고 생각합니다. 전체 TableForm 구성 요소를 업로드하고 감속기를 업데이트했습니다. 제발 도와주세요!
업데이트는 2
나는 mapStateToProps
교체가 마법처럼 일했다. @ JustinTRoss 다시 한번 감사드립니다. 하지만 다른 문제가 있습니다. 셀을 클릭하면 모든 상태가 같은 값으로 나오게됩니다. . 내 계획은 각각의 상태가 자신의 가치가 저장되어 있습니다.
const mapStateToProps = state => {
return {
withSpouseAgePoint: state,
withoutSpouseAge: state,
}
}
내가 해봤'const를 mapDispatchToProps = (파견) => { 반환 { onSelect를 : (점) => {파견 (선택 (점))} } }' – MachoBoy
와'this.props를 추가합니다. onSelect' 클릭 방식 처리. 하지만 로그 콘솔을 사용하면 여전히 정의되지 않은 상태가됩니다. – MachoBoy
@MachoBoy 문제 해결을 위해 mapStateToProps를 {withSpouseAgePoint : state} (으)로 변경하면 기록되는 내용은 무엇입니까? – JustinTRoss