javascript - 我遇到 Material-UI 和 Mobx 的错误,错误 : Material-UI: capitalize(string) expects a string argument

标签 javascript reactjs material-ui mobx mobx-react

在此处输入代码当我从 https://material-ui-next.com/demos/snackbars/ 复制一个 React 组件时遇到此错误 该组件的状态由 Mobx 管理,如下所示:

 @observable snackbarState = {
    open: false,
    vertical: null,
    horizontal: null,
  };

@action toggleSnackState(){
    if(this.snackbarState.vertical){
        console.log('excuted','hidden')
        this.snackbarState={
            open: false,
            vertical: null,
            horizontal: null,
          };
    }else{
        console.log('excuted')
        this.snackbarState={
            open: true,
            vertical: 'top',
            horizontal: 'center',
          };
    }
}

这是我的 snackbar.js:

import React,{Component} from 'react';
import PropTypes from 'prop-types';
import {PropTypes as MobxPropTypes} from 'mobx-react';
import { withStyles } from 'material-ui/styles';
import Snackbar from 'material-ui/Snackbar';
import IconButton from 'material-ui/IconButton';
import CloseIcon from '@material-ui/icons/Close';
import {observer} from 'mobx-react'
const styles = theme => ({
  close: {
    width: theme.spacing.unit * 4,
    height: theme.spacing.unit * 4,
  },
});
@observer 
class SimpleSnackbar extends Component {
    handleClose=()=>{
        console.log(this.context,'---from snackbar')
       this.context.store.toggleSnackState();
    }
  render() {
    const { classes } = this.props;
    let { vertical, horizontal ,open} = this.context.store.snackbarState;
    return (
      <div>
        <Snackbar
          anchorOrigin={{ vertical, horizontal }}
          open={open}
          autoHideDuration={6000}
          onClose={this.handleClose}
          SnackbarContentProps={{
            'aria-describedby': 'message-id',
          }}
          message={<span id="message-id">这里是消息内容</span>}
          action={[
            <IconButton
              key="close"
              aria-label="Close"
              color="inherit"
              className={classes.close}
              onClick={this.handleClose}
            >
              <CloseIcon />
            </IconButton>,
          ]}
        />
      </div>
    );
  }
}
SimpleSnackbar.contextTypes={
    store:MobxPropTypes.observableObject.isRequired
}
SimpleSnackbar.propTypes = {
  classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(SimpleSnackbar);

当我点击 closeButton 时,错误从我的控制台抛出:

 - excuted
 - snackbar.js:18 {store: MyState} "---from snackbar"
 - state.js:58 excuted hidden
 - Error: Material-UI: capitalize(string) expects a string argument.

我对 mobx 的使用有什么问题吗?请帮忙!

最佳答案

Mobx 会将所有原语包装在对象中。这是它可以观察原始值的唯一方法。因为您的 snackbarState 被包裹在一个可观察对象中,所以它的所有后代也被包裹在一个对象中。所以vertical和horizo​​ntal是mobx包裹在对象里的字符串。通常这不会有问题,但是 material ui 检查 capitalize 方法,如果它是字符串类型。它不是,因为它是对象类型(包裹在对象中的字符串)。

因此您需要将可观察对象转换回普通(非包装)js 对象。

import { toJS } from 'mobx';

let { vertical, horizontal, open} = toJS(this.context.store.snackbarState);

请记住,toJS 会创建对象的克隆。因此,当您更改此克隆时,观察者不会更新。

更多文档:

关于javascript - 我遇到 Material-UI 和 Mobx 的错误,错误 : Material-UI: capitalize(string) expects a string argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49811744/

相关文章:

javascript - 部分全景(iPhone 全景)、 Three.js 还是 Pannellum?

javascript:在新窗口的页面上打开所有指向 pdf 的链接

javascript - ReactJS 中 <select> 标签的 onChange 事件的替代方案?

reactjs - 我可以将 Flux 架构应用于 ReactJS.net 吗?

javascript - react 路由器 : access history in rendered Route component

reactjs - 无法在react中获取material-ui SelectField的属性

javascript - 将 this.props.params 传递给子组件

reactjs - 在 react 选择上使用 isClearable 时事件为空

reactjs - Material-UI 侧边菜单?

javascript - 如何从数据库android在recyclerview中显示html.fromhtml文本