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, useEffect} = React;
const api = 'https://gitee.com/api/v5/search/issues?per_page=100'

function FirstDemo() {
  const isCompositionRef = useRef(false);
  const [list, setList] = useState([]);
  const [text, setText] = useState('');
  
  const onChange = (e) => {
    // 输入为 CJK 不执行 getData
    if(isCompositionRef.current) return;
    setText(e?.target?.value);
  }
  const onCompositionStart = (e) => {
    isCompositionRef.current = true;
  }
  const onCompositionEnd = (e) => {
    isCompositionRef.current = false;
    setText(e?.target?.value);
  }
  
  useEffect(() => {
    // setList([]);
    // Create the current request's abort controller
    const abortController = new AbortController();
    // Issue the request
    fetch(`${api}&q=${text}`, {
      signal: abortController.signal,
    })
      .then(res => {
        console.log('resolved')
        // IMPORTANT: we still need to filter the results here,
        // in case abortion happens during the delay.
        // In real apps, abortion could happen when you are parsing the json,
        // with code like "fetch().then(res => res.json())"
        // but also any other async then() you execute after the fetch
        if (abortController.signal.aborted) {
          return;
        }
        let temp = {name: '搜索关键字为' + text};
        setList(list => [...list, temp])
        // setList(res?.data || []);
      })
      .catch(err => {
        console.log('rejected')
        if (abortController.signal.aborted) {
          return;
        }
        let temp = {name: '搜索关键字为' + text};
         setList(list => [...list, temp])
       })
    // Trigger the abortion in useEffect's cleanup function
    return () => {
      abortController.abort();
    };
  }, [text]);

  return <div className="box">
    <div className="search-bar">
      请输入关键字<input 
        onChange={onChange} 
        onCompositionStart={onCompositionStart}
        onCompositionEnd={onCompositionEnd}
      /></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