@abouthalf/

reforward

React

experimenting with forwarded refs

fork
loading
Files
  • src
    • App.js
    • App.css
    • App.test.js
    • index.css
    • index.js
    • logo.svg
  • public
  • package.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.7791166024318195","path":"src/App.js","file":{"path":"src/App.js","content":{"asEncoding":{"base64":"aW1wb3J0IFJlYWN0LCB7IENvbXBvbmVudCB9IGZyb20gJ3JlYWN0JzsKCmltcG9ydCAnLi9BcHAuY3NzJzsKCmNvbnN0IHJhbmRvID0gKG1pbiwgbWF4KSA9PiB7CiAgcmV0dXJuIE1hdGguZmxvb3IoTWF0aC5yYW5kb20oKSoobWF4LW1pbisxKSttaW4pOwp9CgpjbGFzcyBBcHAgZXh0ZW5kcyBDb21wb25lbnQgewoKICBjb25zdHJ1Y3Rvcihwcm9wcykgewogICAgc3VwZXIocHJvcHMpOwogICAgdGhpcy52aWV3ID0gUmVhY3QuY3JlYXRlUmVmKCk7CiAgICB0aGlzLmN1cnJlbnQgPSByYW5kbygxLCAxMDApOwogICAgY29uc29sZS5sb2codGhpcy5jdXJyZW50KTsKICB9CgogIHN0YXRlID0gewogICAgcG9zdHM6IFtdCiAgfQoKICBjb21wb25lbnREaWRNb3VudCgpIHsKICAgIGZldGNoKCJodHRwczovL2pzb25wbGFjZWhvbGRlci50eXBpY29kZS5jb20vcG9zdHMiKQogICAgICAudGhlbihyZXMgPT4gewogICAgICAgIGlmIChyZXMub2spIHsKICAgICAgICAgIHJldHVybiByZXMuanNvbigpCiAgICAgICAgfQogICAgICAgIHJldHVybiBbXQogICAgICB9KQogICAgICAudGhlbihwb3N0cyA9PiB7CiAgICAgICAgdGhpcy5zZXRTdGF0ZSh7IHBvc3RzIH0pCiAgICAgIH0pCiAgICAgIC50aGVuKCgpID0+IHsKICAgICAgICBpZiAodGhpcy52aWV3LmN1cnJlbnQpIHsKICAgICAgICAgIHRoaXMudmlldy5jdXJyZW50LnNjcm9sbEludG9WaWV3KHtiZWhhdmlvcjogInNtb290aCJ9KTsKICAgICAgICB9CiAgICAgIH0pCiAgfQoKICByZW5kZXIoKSB7CiAgICBjb25zdCB7IHBvc3RzIH0gPSB0aGlzLnN0YXRlOwogICAgcmV0dXJuICgKICAgICAgPGRpdiBjbGFzc05hbWU9IkFwcCI+CiAgICAgICAgPHVsPgogICAgICAgICAge3Bvc3RzLm1hcCgocG9zdCwgaSkgPT4gewogICAgICAgICAgICBjb25zdCB7IGlkLCB0aXRsZSwgYm9keSB9ID0gcG9zdDsKICAgICAgICAgICAgbGV0IGluZGV4ID0gaSArIDE7CiAgICAgICAgICAgIGlmIChpbmRleCA9PT0gdGhpcy5jdXJyZW50KSB7CiAgICAgICAgICAgICAgY29uc29sZS5sb2coImN1cnJlbnQiKTsKICAgICAgICAgICAgICByZXR1cm4gPFJlZlBvc3QgcmVmPXt0aGlzLnZpZXd9IGlkPXtpZH0gdGl0bGU9e3RpdGxlfSBib2R5PXtib2R5fSBrZXk9e2l9IC8+CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuIDxQb3N0IGlkPXtpZH0gdGl0bGU9e3RpdGxlfSBib2R5PXtib2R5fSBrZXk9e2l9IC8+CiAgICAgICAgICB9KX0KICAgICAgICA8L3VsPgogICAgICA8L2Rpdj4KICAgICk7CiAgfQp9CgpleHBvcnQgZGVmYXVsdCBBcHA7CgpjbGFzcyBQb3N0IGV4dGVuZHMgQ29tcG9uZW50IHsKICByZW5kZXIoKSB7CiAgICBjb25zdCB7IGlkLCB0aXRsZSwgYm9keSB9ID0gdGhpcy5wcm9wczsKICAgIHJldHVybiAoPGxpPgogICAgICA8aDM+e2lkfToge3RpdGxlfTwvaDM+CiAgICAgIDxwPntib2R5fTwvcD4KICAgIDwvbGk+KQogIH0KfQoKY29uc3QgUmVmUG9zdCA9IFJlYWN0LmZvcndhcmRSZWYoKCBwcm9wcywgcmVmICkgPT4gewogIGNvbnN0IHsgdGl0bGUsIGJvZHkgfSA9IHByb3BzOwogIHJldHVybiA8bGkgcmVmPXtyZWZ9PgogICAgICA8aDM+e3RpdGxlfTwvaDM+CiAgICAgIDxwPntib2R5fTwvcD4KICAgIDwvbGk+Cn0pCg=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React, { Component } from 'react';

import './App.css';

const rando = (min, max) => {
  return Math.floor(Math.random()*(max-min+1)+min);
}

class App extends Component {

  constructor(props) {
    super(props);
    this.view = React.createRef();
    this.current = rando(1, 100);
    console.log(this.current);
  }

  state = {
    posts: []
  }

  componentDidMount() {
    fetch("https://jsonplaceholder.typicode.com/posts")
      .then(res => {
        if (res.ok) {
          return res.json()
        }
        return []
      })
      .then(posts => {
        this.setState({ posts })
      })
      .then(() => {
        if (this.view.current) {
          this.view.current.scrollIntoView({behavior: "smooth"});
        }
      })
  }

  render() {
    const { posts } = this.state;
    return (
      <div className="App">
        <ul>
          {posts.map((post, i) => {
            const { id, title, body } = post;
            let index = i + 1;
            if (index === this.current) {
              console.log("current");
              return <RefPost ref={this.view} id={id} title={title} body={body} key={i} />
            }
            return <Post id={id} title={title} body={body} key={i} />
          })}
        </ul>
      </div>
    );
  }
}

export default App;

class Post extends Component {
  render() {
    const { id, title, body } = this.props;
    return (<li>
      <h3>{id}: {title}</h3>
      <p>{body}</p>
    </li>)
  }
}

const RefPost = React.forwardRef(( props, ref ) => {
  const { title, body } = props;
  return <li ref={ref}>
      <h3>{title}</h3>
      <p>{body}</p>
    </li>
})