小编Lar*_*ars的帖子

'Proptypes'没有定义

我在以下的帮助下建立了一个新的React:https://github.com/facebookincubator/create-react-app

但是,我遇到了一个问题.我收到以下linting错误'PropTypes' is not defined. (no-undef).

以下是导致此问题的代码:

import React, { Component } from 'react';
import PropTypes from 'prop-types';

class Routers extends Component {
  static propTypes = {
    history: PropTypes.object.isRequired
  };

...
Run Code Online (Sandbox Code Playgroud)

我尝试使用react/prop-types规则,但无济于事.

reactjs eslint

20
推荐指数
2
解决办法
3万
查看次数

数组 prop 更新后子组件未更新

当更新数组(对象内部)时,通过向其中添加对象,子组件不会重新渲染。然而,父组件是。

我尝试更新对象的非数组属性,同时也更新对象的数组属性,然后子组件将更新。例如:

不起作用:

obj.arr.push(user);

Run Code Online (Sandbox Code Playgroud)

作品:

obj.arr.push(user);
obj.test = "wow";
Run Code Online (Sandbox Code Playgroud)

我的问题存在于从组件users传递到组件的道具上。当用户加入时,会触发套接字事件,从而修改数组。UsersLobbylobby_player_joinedusers

大厅组件(父级):

...

const StyledTabs = styled(Tabs)`${TabsStyle};`;

class Lobby extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tab: 0,
    };
    this.props.setTitle('Lobby');
  }

  static get propTypes() {
    return {
      history: PropTypes.shape({ push: PropTypes.func.isRequired }).isRequired,
      location: PropTypes.shape({ state: PropTypes.object }).isRequired,
      setTitle: PropTypes.func.isRequired,
      initializeSocket: PropTypes.func.isRequired,
      onceSocketMessage: PropTypes.func.isRequired,
      onSocketMessage: PropTypes.func.isRequired,
      sendSocketMessage: PropTypes.func.isRequired,
    };
  }

  async componentDidMount() {
    await this.props.initializeSocket((error) => {
      console.error(error);
    });

    await this.props.onSocketMessage('exception', (error) => { …
Run Code Online (Sandbox Code Playgroud)

javascript arrays javascript-objects reactjs

2
推荐指数
1
解决办法
4025
查看次数

标签 统计

reactjs ×2

arrays ×1

eslint ×1

javascript ×1

javascript-objects ×1