假設您已配置react-redux propely,這可能是您正在尋找的那個。
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as Actions from '../../actions';
class User extends Component {
constructor(props) {
super(props);
}
click=()=>{
this.props.actions.someEvent();
}
render() {
return (
<div>
(this.props.useravailable) ? <div>Hello world!</div> : <div/>
</div>
);
}
}
Login.PropTypes = {
useravailable: PropTypes.bool.isRequired,
actions: PropTypes.object.isRequired
}
let mapStateToProps = (state,props) => {
return {
useravailable: state.user
}
}
let mapDispatchToProps = (dispatch) => {
return {
actions:bindActionCreators(Actions,dispatch)
};
}
export default connect(mapStateToProps,mapDispatchToProps)(User);
可能*太*寬,因爲你目前有它:)你能夠'連接'狀態變量的組件'道具'? –
@DavinTryon我認爲是的,是的 – Stophface