小编Ema*_*ami的帖子

将模块从devDependencies移动到npm package.json中的依赖项

是否有任何简短的命令将模块移动devDependenciesdependenciespackage.json中?

我发现自己总是这样做:

npm uninstall <module_name> --save-dev 
npm install <module_name> --save
Run Code Online (Sandbox Code Playgroud)

有没有更简短的方法?

npm package.json

116
推荐指数
5
解决办法
2万
查看次数

如何使用 this.refs 获取子组件列表

我有一个<Child />组件列表,然后我使用一个数组this.state.infos来生成这些子组件。如何使用this.refs来获取特定的子组件?

注意:this.state.infos = ['tom', 'mike', 'julie'];例如。

export default class Parent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      infos: {},
    };
  }

  // ignore logic for this.state.infos
  // ...

  render() {
    return (
        <div>
          {[...this.state.infos].map((info) => {
            return <Child
              ref={info}
            />
          })}
        </div>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

reactjs

6
推荐指数
1
解决办法
1万
查看次数

React PureComponent 中的 shouldComponentUpdate 实现

PureComponent在我的 React 应用程序中使用它以获得更好的性能,props但我不希望它在props更改时运行渲染方法。我知道我们不能使用shouldComponentUpdateinReact.PureComponent但我的问题是:

有什么办法可以避免更新React.PureComponent吗?

我希望这个组件根本不更新/渲染。

编辑:shouldComponentUpdate在 pureComponent 中使用时收到此警告:

警告:GameObjectFall 有一个名为 shouldComponentUpdate() 的方法。扩展 React.PureComponent 时不应使用 shouldComponentUpdate。如果使用了 shouldComponentUpdate,请扩展 React.Component。

reactjs

5
推荐指数
1
解决办法
1万
查看次数

在 getDerivedStateFromProps 方法中访问以前的道具

我有与创建的应用程序React 16.3.X,需要this.propsgetDerivedStateFromProps()方法。

例如:

static getDerivedStateFromProps(nextProps, prevState) {
  console.log(this.props);  // `this` is not defined
  if (nextProps.id !== this.props.id) {
    // do something...
  }
  return null;
}
Run Code Online (Sandbox Code Playgroud)

显然this未在规定static的方法,但我需要知道的是有没有办法让this.propsgetDerivedStateFromProps()

reactjs

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

标签 统计

reactjs ×3

npm ×1

package.json ×1