小编xia*_*406的帖子

react-router在子组件中获取this.props.location

据我所知,<Route path="/" component={App} />将提供App与路由相关的道具locationparams.如果我的App组件有许多嵌套的子组件,如何让子组件无需访问这些道具:

  • 从App传递道具
  • 使用窗口对象
  • 为嵌套的子组件创建路由

我以为this.context.router会有一些与路线相关的信息,但this.context.router似乎只有一些功能来操纵路线.

javascript reactjs react-router

56
推荐指数
2
解决办法
7万
查看次数

为什么Airbnb风格指南表示不鼓励依赖功能名称推断?

// bad
class Listing extends React.Component {
  render() {
    return <div>{this.props.hello}</div>;
  }
}

// bad (relying on function name inference is discouraged)
const Listing = ({ hello }) => (
  <div>{hello}</div>
);

// good
function Listing({ hello }) {
  return <div>{hello}</div>;
}
Run Code Online (Sandbox Code Playgroud)

这取自Airbnb反应风格指南.有人可以解释为什么"不鼓励依赖功能名称推断"?这只是一种风格问题吗?

javascript ecmascript-6 reactjs eslint airbnb-js-styleguide

46
推荐指数
3
解决办法
6326
查看次数

处理`window`,`localStorage`的服务器端渲染

当服务器端呈现使用windowlocalStorage这些浏览器全局变量的React组件时,我总是需要添加

if (typeof localStorage !== 'undefined') { // then do stuff }
Run Code Online (Sandbox Code Playgroud)

摆脱那些'localStorage is not defined'错误.

还有其他合适的解决方案吗?

编辑我的用例

  1. window是为了它的属性innerwitdth,并添加原始的浏览器事件,如resize
  2. localStorage 是存储JWT令牌

javascript reactjs server-side-rendering

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

流类型React ref callback和createRef

class Component extends React.Component<ComponentProps> {
  // ...magic
  elementRef = React.createRef();

  render() {
    return this.props.children(this.elementRef);
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的一个组成部分.它基本上将ref回调传递给了孩子们.然后就像这样使用它

<Component>
  {elementRef => (
    <div ref={elementRef} />
  )}
</Component>
Run Code Online (Sandbox Code Playgroud)

我的意图Component是孩子可以附加elementRef到任何元素.

但是我遇到了很多麻烦,无法正确输入我的组件.

我现在在做什么:

type ComponentProps = {
  +children: (
    ref: { current: HTMLDivElement | null } | ((ref: ?HTMLDivElement) => void)
  ) => React.Node,
};
Run Code Online (Sandbox Code Playgroud)

但显然这只适用于div.如果我在跨度上使用elementRef.流会抱怨.

那么我应该如何Component正确键入它以使其适用于任何HTML元素?

reactjs flowtype

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

即使将Border设置为EmptyBorder,JButton的边框也不会消失

我正在使用Mac上的Eclipse设计GUI,我想让我JButton只显示我设置的Icon.它在Mac OS上看起来很好,但是当我将它导出到jar文件并在Windows操作系统上运行时,JButton看起来像这样:

在此输入图像描述

边界都是EmptyBorder.

我做错了什么?怎么能让后面的广场消失?

java user-interface swing jbutton imageicon

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