使用Styles从连接的Redux组件获取引用

时间:2019-07-24 20:48:41

标签: reactjs react-redux material-ui

我导出了一个工作组件:

export default connect(
    mapStateToProps, actions,
    null, { withRef: true, forwardRef: true }
  )(withTheme()(withStyles(styles)(MainMenu)));

及其调用:

<MainMenu 
  ref={(connectedMenu) => this.menuRef = connectedMenu.getWrappedInstance()} 
  user={user} 
/>

我期望获得MainMenu引用,但我一直在获取WithTheme对象。

我也尝试通过innerRef,但是遇到以下错误:

TypeError: connectedMenu.getWrappedInstance is not a function
TypeError: Cannot read property 'getWrappedInstance' of null

在所有这些操作之前,我都尝试过React.createRef()格式,但是没有用。

我如何获得此推荐?

1 个答案:

答案 0 :(得分:0)

假设您正在使用Material-UI的v4,则withTheme的语法不正确。在v4中,第一组括号was removed

代替

withTheme()(YourComponent)

您应该拥有

withTheme(YourComponent)

下面是react-redux todo list tutorial的修改版本中的代码,显示了正确的语法。我在此处包括了我更改过的两个文件(TodoList.js和TodoApp.js),但是沙箱是一个完全正常的示例。

TodoApp中,我使用TodoList上的ref获取并显示其高度。仅当TodoApp重新渲染时,显示的高度才会更新,因此我添加了一个按钮来触发重新渲染。如果将几个待办事项添加到待办事项列表中,然后单击“重新渲染”按钮,您将看到显示了列表的新高度(表明参考已完全正常工作)。

TodoList中,我正在使用withStyles在待办事项列表周围添加蓝色边框以显示withStyles在工作,并且正在显示主题的原色以表明withTheme正常工作。

TodoList.js

import React from "react";
import { connect } from "react-redux";
import Todo from "./Todo";
import { getTodosByVisibilityFilter } from "../redux/selectors";
import { withStyles, withTheme } from "@material-ui/core/styles";
import clsx from "clsx";

const styles = {
  list: {
    border: "1px solid blue"
  }
};
const TodoList = React.forwardRef(({ todos, theme, classes }, ref) => (
  <>
    <div>theme.palette.primary.main: {theme.palette.primary.main}</div>
    <ul ref={ref} className={clsx("todo-list", classes.list)}>
      {todos && todos.length
        ? todos.map((todo, index) => {
            return <Todo key={`todo-${todo.id}`} todo={todo} />;
          })
        : "No todos, yay!"}
    </ul>
  </>
));

const mapStateToProps = state => {
  const { visibilityFilter } = state;
  const todos = getTodosByVisibilityFilter(state, visibilityFilter);
  return { todos };
};
export default connect(
  mapStateToProps,
  null,
  null,
  { forwardRef: true }
)(withTheme(withStyles(styles)(TodoList)));

TodoApp.js

import React from "react";
import AddTodo from "./components/AddTodo";
import TodoList from "./components/TodoList";
import VisibilityFilters from "./components/VisibilityFilters";
import "./styles.css";

export default function TodoApp() {
  const [renderIndex, incrementRenderIndex] = React.useReducer(
    prevRenderIndex => prevRenderIndex + 1,
    0
  );
  const todoListRef = React.useRef();
  const heightDisplayRef = React.useRef();
  React.useEffect(() => {
    if (todoListRef.current && heightDisplayRef.current) {
      heightDisplayRef.current.innerHTML = ` (height: ${
        todoListRef.current.offsetHeight
      })`;
    }
  });
  return (
    <div className="todo-app">
      <h1>
        Todo List
        <span ref={heightDisplayRef} />
      </h1>
      <AddTodo />
      <TodoList ref={todoListRef} />
      <VisibilityFilters />
      <button onClick={incrementRenderIndex}>
        Trigger re-render of TodoApp
      </button>
      <div>Render Index: {renderIndex}</div>
    </div>
  );
}

Edit Todo App with Redux