页面中的计数概念递归问题

发布于 2025-01-22 22:33:46 字数 1710 浏览 0 评论 0原文

我有一个概念上的问题,我有一个非常简单且最小的代码框:

我想保留组件的 在我的页面中(每个组件)。 这些是我的组成部分。 (我正在使用preactect,但我们在这里确实在谈论功能)

function Element() {
  return " " //I'm not important
}
function Container() {
    //container is repeating my Element
    return <div>
            <Element />
            <Element />
            <Element />
            <Element />
           </div>   
}

我在页面中保留 element 容器的计数仅在第一个(firstType = true)时,才能在组件中添加属性来跟踪计数。

我想这样做,因为这种类型的组件需要第一次渲染。 (&lt; defs&gt;对于SVG)

此高阶功能轻松地做到这一点

function Counter(Component) {
  let count = 0;
  return function (...props) {
   //this is for the purpose of the debugging
    return <>{++count} <Component {...props[0]} /> </>
  }
}

我可以通过 组件,因此我可以知道它的宽度和高度(我也可以通过对DOM节点和GetBbox调用的参考来做到这一点,但我希望我的应用程序在没有浏览器的情况下工作)。

我最好的选择是将组件转换为字符串,获取路径的点,然后使用PathCommander的服务器端GetBbox或其他一些SSR技术(如Phantomjs)来测量路径。

这一切都会很好地很好,但是再次在我的组件上运行rendertosticticstring时,组件 本身也通过rendertostaticstring被称为。我拥有的组件)。

这是因为这次,当我称我的组件函数时,我称其为只是为了测量即时的路径,不渲染< /strong>像我对正常react.render()或正常的SSR渲染情况所做的那样进入页面中的组件。 因此,我需要将计数重置为0后进行渲染。因此,在页面上,它仍然可以正确地行动,但我在没有运气的情况下尝试了一切。

我真的想不出一个简单正确的解决方案,可以解决这个难题,如果有人有一些起始想法或一些有用的信息,请分享! 在这里做了一个可以使用的代码和盒子(只需输入// rendertosticticstring即可使其停止工作(您必须手动重新加载或Autosave不会将计数重置为0(这是因为它不是在此处的SSR)。

I have this conceptual problem and I have a really simple and minimal codesandbox for it:

I want to keep a count of the components that are rendered in my page (per component).
These are my components. (I'm working with Preact but we're really talking about functions here)

function Element() {
  return " " //I'm not important
}
function Container() {
    //container is repeating my Element
    return <div>
            <Element />
            <Element />
            <Element />
            <Element />
           </div>   
}

I keep the count of Element and Container in the page with an High Order Function that keeps track of the count by adding a property to the component only if it's the first one (firstType = true).

I wanna do this because this type of component needs to render differently the first time. (< defs> for svg)

I can do that easily with this High Order Function

function Counter(Component) {
  let count = 0;
  return function (...props) {
   //this is for the purpose of the debugging
    return <>{++count} <Component {...props[0]} /> </>
  }
}

The problem occured when in another HOC I tried to render the component with renderToStaticString(),

P.S. I need to that on my Component so I can know its width and height (I could do this also with a ref to the dom node and a getBBox call but I want my application to work without a browser).

My best option would be converting the component to a string, getting the points of the path and then measure the path with PathCommander's server-side getBBox or maybe some other ssr techniques like PhantomJS.

This would all work relly nice but again when I run renderToStaticString on my component, the component itself gets also called by renderToStaticString and therefore the Counting HOC gets involved in it ruining the count of the components I had).

This is because this time, when I'm calling my component function, I'm calling it only to measure the path on the fly, not to render the component into the page like I would have done with a normal React.render() or in a normal ssr rendering situation.
So I need to reset the count to 0 after doing this render on the fly. SO on the page it will still act correctly but I tried everything with no luck.

I seriously can't think of a simple and correct solution that would solves this puzzle all togheter, if anyone has some starting ideas or some useful infos please SHARE IT! Here I also did a codesandbox where you can play with (just uncomment the //renderToStaticString to make it stop working (You have to manually reload or autosave won't reset the count to 0 (this is because it's not ssr here ofc).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2025-01-29 22:33:46

一种技术可能是创建参考react.createelement并使用包装函数覆盖它,该功能会增加elementCount

function App() {
  return <div>
    <p>Hello world</p>
    <p>Another paragraph and <span>here</span></p>
    <p>This is the sixth element, counting the outermost App</p>
  </div>
}

let elementCount = 0
const createElement = React.createElement
React.createElement = (...args) => {
  elementCount++
  return createElement(...args)
}

ReactDOM.render(<App/>, document.querySelector("#app"))
console.log("total elements", elementCount)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.14.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.14.0/umd/react-dom.production.min.js"></script>
<div id="app"></div>

total elements 6

这计算了六(6)个元素。

  1. &lt; app&gt;
  2. &lt; div&gt;
  3. &lt; p&gt;
  4. 代码>&lt; span&gt;
  5. &lt; p&gt;这是第六个...

不确定它是否与rendertostaticstring一起使用。尝试一下,让我们知道。

One technique might be to create a reference React.createElement and override it with a wrapper function that increments an elementCount.

function App() {
  return <div>
    <p>Hello world</p>
    <p>Another paragraph and <span>here</span></p>
    <p>This is the sixth element, counting the outermost App</p>
  </div>
}

let elementCount = 0
const createElement = React.createElement
React.createElement = (...args) => {
  elementCount++
  return createElement(...args)
}

ReactDOM.render(<App/>, document.querySelector("#app"))
console.log("total elements", elementCount)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.14.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.14.0/umd/react-dom.production.min.js"></script>
<div id="app"></div>

total elements 6

This counts six (6) elements.

  1. <App>
  2. <div>
  3. <p>Hello world
  4. <p>Another paragraph and
  5. <span>
  6. <p>This is the sixth...

Not sure if it will work with renderToStaticString or not. Try it and let us know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文