首页 > 解决方案 > 在代码沙箱中使用 npm 包对 web worker 做出反应

问题描述

https://codesandbox.io/s/redux-async-actions-y5ho5

<DataList
        doFetch={store.getState().fetched}
        data={store.getState().data}
      />

      <MyComponent />


const MyComponent = () => (
  <WebWorker url="/worker.js">
    let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,

  19, 20, 21,
];
let result= [];

let plantsSearchData = numbers.map(search => {
 // console.log("numbers---->", search);
  if (search.length > 1) {
            result.push(search);
  }

   console.log("pushed numbers---->", result);

});




    const response = await axios.get( "https://reqres.in/api/users?page=2"
    );
    {({ data, error, postMessage, updatedAt, lastPostAt }) => (
      <div>
        {data && (
          <div>
            <strong>Received some data:</strong>
            <pre>{JSON.stringify(data, null, 2)}</pre>
          </div>
        )}
        <button
          onClick={() => postMessage("hello")}
          disabled={updatedAt < lastPostAt}
        >
          {updatedAt < lastPostAt ? "Loading..." : "Go"}
        </button>
      </div>
    )}
  </WebWorker>
);

标签: javascripthtmlreactjsreduxweb-worker

解决方案


在您的 main.js 文件中导入 web-worker 并在您的 componentDidMount() 生命周期中创建一个 webworker 实例

使用此链接获取更多信息网络工作者反应


推荐阅读