【发布时间】:2017-03-31 17:06:18
【问题描述】:
我有一个使用state 向用户提供数据的组件。例如<div>this.state.variableInState</div>。该组件可以调度某些方法(例如在onClick 操作上)。我目前正在使用react-redux 和connect 方法将store 映射到props。有没有办法可以在发送后setState?
// actions
export function executeAction() {
return function (dispatch, getState) {
dispatch({
type: 'MY_ACTION',
payload: axios.get('/some/url')
});
};
}
// reducer
export default function (state = {}, action) {
switch (action.type) {
case 'MY_ACTION_FULFILLED':
return {...state, myVariable: action.payload.data}
}
}
//component
class MyComponent extends Component {
render() {
(<div onClick={this.props.executeAction.bind(this)}>
{this.state.variableInState}
</div>)
}
someOtherMethod() {
// I want to operate with state here, not with props
// that's why my div gets state from this.state instead of this.props
this.setState({variableInState: 'someValue'})
}
}
export default connect((state, ownProperties) => {
return {
// So I want to change MyComponent.state.variableInState here
// but this method updates MyComponent props only
// What can I do?
variableInProps: state.myVariable
}
}, {executeAction})(MyComponent);
【问题讨论】:
标签: javascript reactjs redux react-redux