我正在尝试将我的组件 CMSForm 连接到 Redux 商店,但无法这样做。这是我的:
减速器:
const cmsDefaultState = {
cmsNum: "",
facilityName: ""
};
export default (state = cmsDefaultState, action) => {
switch (action.type){
case 'SET_CMS_ID':
return Object.assign({}, state, {
cmsNum: action.cmsNum
});
case 'SET_FACILITY_NAME':
return [
...state,
action.facilityName
];
default:
return state;
}
};
店铺:
import { createStore} from 'redux'
import CMSReducer from './CMSReducer.js'
export default () => {
const store = createStore(CMSReducer);
return store;
};
选择器:
export const getCMSNum = store =>
store.cmsNum
export const getFacilityName = store =>
store.facilityName
方法:
export const setCMSId = (cmsNum = '') => ({
type: 'SET_CMS_ID',
cmsNum
});
export const setFacilityName = (facilityName = '') => ({
type: 'SET_FACILITY_NAME',
facilityName
});
这是组件:
import React from 'react'
import { connect } from 'react-redux';
import { setCMSId } from '../redux/methods'
import { getCMSNum } from "../redux/selectors";
function mapStateToProps(state) {
const { CMSReducer } = state
return { CMSForm: CMSReducer }
}
class CMSForm extends React.Component {
constructor(props) {
super(props);
this.state = {
cmsCertificationNumber: '',
facility_name: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleCmsNumber = this.handleCmsNumber.bind(this);
}
handleCmsNumber = () => {
// dispatches actions to add todo
debugger
setCMSId(this.state.cmsCertificationNumber);
alert("CMS Number: " + this.state.cmsNum);
} // end of HandleCMSNumebr
handleChange(event) {
this.setState({cmsCertificationNumber: event.target.value});
}
render() {
return (
<div>
<input type="text" value={this.state.cmsCertificationNumber} onChange={this.handleChange} />
<button className="update-cmsNum" onClick={this.handleCmsNumber}>
Find
</button>
</div>
)
}
芜湖不芜
相关分类