小编Pub*_*nka的帖子

可以在按钮反应的 onClick 中调用异步函数吗

我将onclick在按钮中的事件中调用异步函数。但它不起作用。是否可以在按钮 onlick 中调用异步函数?

这是代码。

过滤按钮

const FilterButton = (props) => {

return (
    <div className="p-2 ">
        <button className="btn btn-secondary" onClick={props.fetchInvoices}>Filter</button>
    </div>
 );
};
Run Code Online (Sandbox Code Playgroud)

fetchInvoices 异步函数

fetchInvoices = async () => {
    const invoices = await getInvoices(this.currentState.params);
    this.props.store.dispatch(UpdateInvoices(invoices));
};
Run Code Online (Sandbox Code Playgroud)

将函数传递给组件的代码

 <SearchField store = {this.props.store} fetchInvoices={this.fetchInvoices}/>
Run Code Online (Sandbox Code Playgroud)

reactjs

21
推荐指数
2
解决办法
3万
查看次数

如何在 Axios Mock 适配器中使用路径参数

我正在使用 axios 模拟适配器来模拟我的反应前端的数据。目前我正在使用 param 并且它正在工作。但我需要支持它跟随网址

.../发票/1

这是我的代码

let mock;
if (process.env.REACT_APP_MOCK_ENABLED === 'true') {
console.log('Simulation mode is enabled ');
mock = new MockAdapter(axios);

mock
    .onGet(apiUrl + '/invoice').reply(
    (config) => {
        return [200, getMockInvoice(config.params)];
    })
    .onGet(apiUrl + '/invoices').reply(
    (config) => {
        return [200, getMockInvoices(config.params)];
    });
    }
Run Code Online (Sandbox Code Playgroud)
export const getInvoice = async (id) => {
console.log(id);
try {
    const invoiceResponse = await axios.get(apiUrl + `/invoice/${id}`);
    return invoiceResponse.data;
} catch (e) {
    console.log(e);
 }
};
Run Code Online (Sandbox Code Playgroud)
export const getMockInvoice = (params) => {
let …
Run Code Online (Sandbox Code Playgroud)

javascript testing axios axios-mock-adapter

7
推荐指数
1
解决办法
4406
查看次数

如何通过偏移和限制来反应无限查询支持

我尝试使用react-queryreact-virtual中的无限查询在我的react.js项目中实现无限滚动。但无限查询支持游标和页面。我的 API 不支持页面,它在元数据中有一个限制、偏移量和总计数,如下所示

meta: { limit: 100, offset: 0, total: 1000}

无限查询是否支持限制和偏移?

我关注了一些链接。

https://codesandbox.io/s/github/tannerlinsley/react-virtual/tree/master/examples/infinite-scroll

https://react-query.tanstack.com/docs/guides/infinite-queries

infinite-scroll reactjs react-query

6
推荐指数
1
解决办法
5100
查看次数

本地主机发送了无效的响应。ERR_INVALID_HTTP_RESPONSE

我正在努力在 nginx docker 中启用 http2。当我调用 localhost 时,我收到此错误。我的 nginx 配置文件如下。

server {
listen       2020 http2;
server_name  localhost;

#charset koi8-r;
#access_log  /var/log/nginx/host.access.log  main;

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP …
Run Code Online (Sandbox Code Playgroud)

nginx http2

5
推荐指数
1
解决办法
2万
查看次数