如果MyComponent从redux存储中获取数据,但是在映射之前先以某种方式对其进行组织,那么该组织应该在component或mapStateToProps函数中完成吗?为什么?
const MyComponent = ({ data }) => {
// IN HERE?
return (
<div>
{data.map((d) => (...))}
</div>
);
};
const mapStateToProps = (state) => {
const output = state.data
// OR HERE?
return { data: output };
};
export default connect(mapStateToProps)(MyComponent);
答案 0 :(得分:0)
你好。
我认为最好有一个文件来与redux联系,因此每次需要与redux连接时,我都会创建一个名为ComponentNameContainer.jsx的文件,该文件如下所示:
import { connect } from 'react-redux';
import { withRouter } from 'react-router';
import Row from '../components/Row';
import {doSomething} from '../redux/somethingActions'
// here the imports of function from your actions
export default withRouter(connect(
(state, ownProps) => {
return {
// props that u need from redux
// example: state.sessionReducer.user
}
},
{
// functions that u need from redux
//example: doSomething
}
)(Row))
我有一个文件夹调用容器,用于存储所有容器文件,以跟踪与redux连接的组件。