Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                import * as React from "https://cdn.skypack.dev/react@17.0.1";
import * as ReactDOM from "https://cdn.skypack.dev/react-dom@17.0.1";
import "https://cdn.skypack.dev/whatwg-fetch";
const {useState, useRef} = React;
const api = 'https://gitee.co/api/v5/search/issue?per_page=100'

function FirstDemo() {
  // fetch 返回 promise,只保存最后一个 promise
  const lastPromise = useRef();
  const [list, setList] = useState([]);
  const isCompositionRef = useRef(false);
  
  const getData = (e) => {
    const currentPromise = fetch(api, )
    console.log(currentPromise)
    lastPromise.current = currentPromise
    // 过滤掉不是最后一次发起的请求
    currentPromise.then(
      res => {
        if (currentPromise === lastPromise.current) {
          setList(res?.data || []);
        }
      },
      err => {
        if (currentPromise === lastPromise.current) {
          let temp = {name: '搜索关键字为' + e.target.value};
          setList(list => [...list, temp])
          console.warn('fetch failure', e);
        }
      },
    );
  }
  const onChange = (e) => {
    // 输入为 CJK 不执行 getData
    if(isCompositionRef.current) return;
    getData(e)
  }
  const onCompositionStart = (e) => {
    isCompositionRef.current = true;
  }
  const onCompositionEnd = (e) => {
    isCompositionRef.current = false;
    getData(e)
  }

  return <div className="box">
    <div className="search-bar">
      你可以快速输入中文或英文:<input 
        onChange={onChange} 
      /></div>
    <p style={{color: 'red'}}>我们只处理最后一次请求的响应</p>
    <ul className="list-box">
      {list?.map((v, i) => {
        return <li>{v.name}</li>
      })}
    </ul>
  </div>
}


ReactDOM.render(<FirstDemo />, document.getElementById('app'));
              
            
!
999px

Console