带有Redux连接的TypeScript传递额外的道具

时间:2017-10-23 05:17:12

标签: typescript redux react-redux

如何正确传递额外的道具?

r=20; c=20;                         
filt=zeros(r*2+1, c*2+1); filt(end)=1;
img2=conv2(img,filt,'same');
figure; imshow(img2,[])

我想传递className

const SideBar = connect(
  (state: State) => ({current: state.projects.current}),
  null
)(({className, current}) => (
  <nav className={'side-bar ' + className}>
    ...
  </nav>
))

我收到了这个错误:

<SideBar className="app__sidebar"/>

1 个答案:

答案 0 :(得分:4)

我认为,问题是你应该在mapStateToProps函数中使用ownProps并将className prop传递给组件显然

function mapStateToProps(state: State, ownProps) {
  return {
    current: state.projects.current,
    className: ownProps.className
  }
}

const SideBar = connect(mapStateToProps, null)(({className, current}) => (
  <nav className={'side-bar ' + className}>
    ...
  </nav>
))