简体   繁体   English

Reactjs 没有按预期呈现条件输出

[英]Reactjs does not render conditional output as expected

I have already published a question regarding my problem but this is still not solved:我已经发布了一个关于我的问题的问题,但这仍然没有解决:

Reactjs socks does not apply down mode to rendered output Reactjs 袜子不会将向下模式应用于渲染输出

I have following code (please scroll down to the same bottom):我有以下代码(请向下滚动到同一底部):

import React from "react";
import { setDefaultBreakpoints } from "react-socks";
import Breakpoint, { BreakpointProvider } from "react-socks";

class BreakpointComponent extends React.Component {
  constructor(props) {
    super(props);
  }

  componentWillMount() {
    setDefaultBreakpoints([{ mobile: 900 }]);
  }

  render() {
    return (
      <BreakpointProvider>
        <Breakpoint mobile down>
          <MobileComponent />
        </Breakpoint>

        <Breakpoint mobile up>
          <DesktopComponent />
        </Breakpoint>
      </BreakpointProvider>
    );
  }
}

class DesktopComponent extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div>
        <h1>I WILL RENDER ON DESKTOP</h1>
      </div>
    );
  }
}

class MobileComponent extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div>
        <h1>I WILL RENDER ON MOBILE</h1>
      </div>
    );
  }
}

export default BreakpointComponent;

I obtain following output:我获得以下输出:

  • for desktop sizes narrower than 900px I receive expected output:对于小于 900px 的桌面尺寸,我收到预期输出:

    I WILL RENDER ON MOBILE我会在手机上渲染

  • for desktop sized wider than 900px I unfortunately receive unexpected output:对于尺寸大于 900px 的桌面,我不幸收到意外输出:

    I WILL RENDER ON MOBILE我会在手机上渲染

    I WILL RENDER ON DESKTOP我会在桌面上渲染

This is not what I expect.这不是我所期望的。

Maybe I have problems because React Socks is not compatible with React Router.也许我有问题,因为 React Socks 与 React Router 不兼容。 This is how I place it in App.js React root file:这是我将它放在 App.js React 根文件中的方式:

// ...
return (
  <BrowserRouter>
    <div>
      <Switch>
        <Route path="/breakpoint" render={props => <BreakpointComponent {...props} />} exact />
      </Switch>
    </div>
  </BrowserRouter>
);
// ...

I do not want to receive output I desire for screen sizes wider than 900px together with this I want to receive when the current screen size is narrower than 900px.我不想接收我希望屏幕尺寸大于 900px 的输出以及当当前屏幕尺寸小于 900px 时我想要接收的输出。

What am I missing?我错过了什么?

So react-socks define the modifiers you can use as follows所以 react-socks 定义了你可以使用的修饰符如下

You have three modifiers你有三个修饰符

• only - will render the component only in the specified breakpoint. • only - 将仅在指定的断点处呈现组件。

• up - will render the component in the specified breakpoint and all the breakpoints above it (greater than the width). • up - 将在指定断点及其上方的所有断点(大于宽度)中渲染组件。

• down - will render the component in the specified breakpoint and all the breakpoints below it (less than the width). • down - 将在指定的断点及其下方的所有断点(小于宽度)渲染组件。

Notice that up renders the component in the breakpoint and above it.请注意, up断点及其上方呈现组件。

If we look at your default breakpoints you only have 'mobile: 900', this means the down modifier will take that breakpoint (900-infinity) and the one below it (0-900).如果我们查看您的默认断点,您只有“移动:900”,这意味着向下修饰符将采用该断点(900-无穷大)及其下方的断点(0-900)。

To do what you want you should use more than one breakpoint, I think react-socks kinda expects that tbh.为了做你想做的事,你应该使用多个断点,我认为 react-socks 有点期望 tbh.

So if you have breakpoints like mobile: 600, desktop:900.所以如果你有像 mobile:600,desktop:900 这样的断点。 The mobile component could take the mobile breakpoint (600-900) down to 0. And the desktop component could take the desktop breakpoint up (900-infinity)移动组件可以将移动断点 (600-900) 降低到 0。桌面组件可以将桌面断点向上 (900-无穷大)

I'm not quite sure which react-socks version that you are using.我不太确定您使用的是哪个react-socks版本。 But according to the latest documentation, you have to use it as follows.但是根据最新的文档,你必须按如下方式使用它。

return (
<div>
  <Breakpoint small down>
    <div>I will render only in mobile devices</div>
  </Breakpoint>

  <Breakpoint medium only>
    <div>I will render only in tablets (iPad, etc...)</div>
  </Breakpoint>

  <Breakpoint medium down>
    <div>I will render in tablets (iPad, etc...) and everything below (mobile devices)</div>
  </Breakpoint>

  <Breakpoint medium up>
    <div>I will render in tablets (iPad, etc...) and everything above (laptops, desktops)</div>
  </Breakpoint>

  <Breakpoint large up>
    <div>I will render in laptops, desktops and everything above</div>
  </Breakpoint>
</div>);

You are using the word 'mobile' instead of keywords small,medium,large ;您使用的是“移动”一词,而不是关键字small,medium,large

The convention followed by react-socks is kinda weird. react-socks遵循的约定有点奇怪。 I couldn't get clear idea from their documentation.我无法从他们的文档中得到清晰的想法。 If you observe keenly they have a 0 breakpoint in all their examples.如果您仔细观察,他们在所有示例中都有一个0断点。 That should give you some idea.这应该给你一些想法。 If you say mobile that has to be a range from the width specified for that breakpoint to the next breakpoint.如果您说mobile必须是从为该断点指定的宽度到下一个断点的范围。 In your case you have only one break point so the behaviour can not be defined well.在您的情况下,您只有一个断点,因此无法很好地定义行为。 Try with 2 breakpoints {mobile: 0, desktop: 900} and use mobile only and desktop up or desktop only .尝试使用 2 个断点{mobile: 0, desktop: 900}并使用mobile only and desktop up or desktop only

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM