有条件地从父母那里禁用孩子的按钮-React

罗伯特·霍汉尼斯扬

我有一个Paginator子组件,如果要在第一页上禁用左箭头按钮,如果在第一页上方,则要再次单击它。右箭头按钮也一样,但页面编号为5。

这是我尝试为此解决方案编写的条件:

if (this.state.page <= 1) {
  this.setState({
    disabledPrev: true
  })
} else {
  this.setState({
    disabledPrev: false
  })
}

if (this.state.page >= 5) {
  this.setState({
    disabledNext: true
  })
} else {
  this.setState({
    disabledNext: false
  })
}

但这不管我放在哪里都不管用。我认为问题出在生命周期方法的错误使用上。

这是整个代码:

App.js

import React from 'react';
import './App.css';

import Loading from './components/Loading/Loading';
import Header from './components/Header/Header';
import Table from './components/Table/Table';
import Paginator from './components/Paginator/Paginator';

const BASE_URL = 'https://api.udilia.com/coins/v1/'
export default class App extends React.Component{ 
  constructor(props) {
    super(props);
    console.log('constructor');
    this.state = {
      currencies: null,
      isDataReady: false,
      page : 1,
      disabledPrev: false,
      disabledNext: false,
    }
  }

  fetchCurrencies = (pageNumber) => {
    fetch(`${BASE_URL}cryptocurrencies?page=${pageNumber}&perPage=20`)
      .then(res => res.json())
      .then(json => json.currencies)
      .then(currencies => {
        this.setState({
          currencies: currencies,
          isDataReady: true,
        })
      });
  }

  UNSAFE_componentWillMount() {
    console.log('will mount', this.state.page);

  }

  componentDidUpdate() {
    console.log('didUpdate', this.state.page);

  }

  componentDidMount() {
    console.log('did mount', this.state.page);

    //Here is the conditions
    if (this.state.page <= 1) {
      this.setState({
        disabledPrev: true
      })
    } else {
      this.setState({
        disabledPrev: false
      })
    }

    if (this.state.page >= 5) {
      this.setState({
        disabledNext: true
      })
    } else {
      this.setState({
        disabledNext: false
      })
    }

    const {page} = this.state;

    this.fetchCurrencies(page);

  }

  //here is the event handler, I change the page value from here
  handlePaginationClick  = (direction) => () => {
    console.log('clicked', this.state.page);

    let page = direction === 'next' ? this.state.page + 1 : this.state.page - 1;

    this.setState({page})
    this.fetchCurrencies(page);
  }

  render(){
    console.log('render', this.state.page);

    return (
        !this.state.isDataReady
          ? <Loading />
          : <div className="App">
              <Header />
              <Table
                data={this.state.currencies}
              />
              <Paginator 
                prev={this.handlePaginationClick('prev')}
                next={this.handlePaginationClick('next')}
                page={this.state.page}
                disabledPrev={this.state.disabledPrev}
                disabledNext={this.state.disabledNext}
              />

            </div>
    );
  }
}

Paginator.js

import React from 'react';

export default function Paginator(props) {
    return(
        <div>
            <button disabled={props.disabledPrev} onClick={() => props.prev()}> &larr; </button>
            <span>page {props.page} of 10</span>
            <button disabled={props.disabledNext} onClick={() => props.next()}> &rarr; </button>
        </div>
    )
}
普拉卡什S

尝试这个

import React from 'react';
import './App.css';

import Loading from './components/Loading/Loading';
import Header from './components/Header/Header';
import Table from './components/Table/Table';
import Paginator from './components/Paginator/Paginator';

const BASE_URL = 'https://api.udilia.com/coins/v1/'
export default class App extends React.Component{ 
  constructor(props) {
    super(props);
    console.log('constructor');
    this.state = {
      currencies: null,
      isDataReady: false,
      page : 1
    }
  }

  fetchCurrencies = (pageNumber) => {
    fetch(`${BASE_URL}cryptocurrencies?page=${pageNumber}&perPage=20`)
      .then(res => res.json())
      .then(json => json.currencies)
      .then(currencies => {
        this.setState({
          currencies: currencies,
          isDataReady: true,
        })
      });
  }

  UNSAFE_componentWillMount() {
    console.log('will mount', this.state.page);

  }

  componentDidUpdate() {
    console.log('didUpdate', this.state.page);

  }

  componentDidMount() {
    console.log('did mount', this.state.page);
    const {page} = this.state;
    this.fetchCurrencies(page);

  }

  //here is the event handler, I change the page value from here
  handlePaginationClick  = (direction) => () => {
    console.log('clicked', this.state.page);

    let page = direction === 'next' ? this.state.page + 1 : this.state.page - 1;

    this.setState({page})
    this.fetchCurrencies(page);
  }

  render(){
    console.log('render', this.state.page);
    const {page} = this.state;
    const disabledPrev = page <=1 ? true : false;
    const disabledNext = page >=5 ? true : false;
    return (
        !this.state.isDataReady
          ? <Loading />
          : <div className="App">
              <Header />
              <Table
                data={this.state.currencies}
              />
              <Paginator 
                prev={this.handlePaginationClick('prev')}
                next={this.handlePaginationClick('next')}
                page={page}
                disabledPrev={disabledPrev}
                disabledNext={disabledNext}
              />

            </div>
    );
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用React Redux状态有条件地显示和禁用按钮

来自分类Dev

有条件地禁用React Checkbox

来自分类Dev

有条件地禁用滚动到顶部的dom change react router

来自分类常见问题

如何有条件地包装React组件?

来自分类Dev

在React中有条件地设置活动类

来自分类Dev

有条件地与react-router链接

来自分类Dev

在React中有条件地添加HTML属性

来自分类Dev

使用React有条件地停止游戏循环

来自分类Dev

有条件地在react jsx中渲染className

来自分类Dev

在React中有条件地设置活动类

来自分类Dev

React-native - 有条件地设置高度

来自分类Dev

在 React 组件中有条件地渲染 <td>

来自分类常见问题

如何在React Native底部选项卡中有条件地更改屏幕按钮?

来自分类Dev

如何在React Native底部选项卡中有条件地更改屏幕按钮?

来自分类Dev

如何检查id是否等于用户的id并在react js中有条件地显示按钮?

来自分类Dev

有条件地设置 React 中所有 html 标签的样式

来自分类Dev

使用React Router当前路由在菜单上有条件地设置活动类

来自分类Dev

如何在React Native Component中有条件地包含图像?

来自分类Dev

在React.js中有条件地设置html属性

来自分类Dev

在React JSX中有条件地渲染组件部分

来自分类Dev

如何在React JS中有条件地呈现错误?

来自分类Dev

React JS在上传时有条件地渲染动画乐透组件

来自分类Dev

我如何有条件地包装此React组件

来自分类Dev

如何有条件地渲染使用钩子的React组件

来自分类Dev

根据组件的使用位置,有条件地将className应用于react组件

来自分类Dev

有条件地渲染JSX的React组件的更新周期如何表现?

来自分类Dev

根据React功能组件中的状态变量有条件地设置className

来自分类Dev

如何有条件地设置语义UI React Grid列的width属性?

来自分类Dev

使用Firebase自定义声明有条件地在React Native中呈现UI

Related 相关文章

  1. 1

    如何使用React Redux状态有条件地显示和禁用按钮

  2. 2

    有条件地禁用React Checkbox

  3. 3

    有条件地禁用滚动到顶部的dom change react router

  4. 4

    如何有条件地包装React组件?

  5. 5

    在React中有条件地设置活动类

  6. 6

    有条件地与react-router链接

  7. 7

    在React中有条件地添加HTML属性

  8. 8

    使用React有条件地停止游戏循环

  9. 9

    有条件地在react jsx中渲染className

  10. 10

    在React中有条件地设置活动类

  11. 11

    React-native - 有条件地设置高度

  12. 12

    在 React 组件中有条件地渲染 <td>

  13. 13

    如何在React Native底部选项卡中有条件地更改屏幕按钮?

  14. 14

    如何在React Native底部选项卡中有条件地更改屏幕按钮?

  15. 15

    如何检查id是否等于用户的id并在react js中有条件地显示按钮?

  16. 16

    有条件地设置 React 中所有 html 标签的样式

  17. 17

    使用React Router当前路由在菜单上有条件地设置活动类

  18. 18

    如何在React Native Component中有条件地包含图像?

  19. 19

    在React.js中有条件地设置html属性

  20. 20

    在React JSX中有条件地渲染组件部分

  21. 21

    如何在React JS中有条件地呈现错误?

  22. 22

    React JS在上传时有条件地渲染动画乐透组件

  23. 23

    我如何有条件地包装此React组件

  24. 24

    如何有条件地渲染使用钩子的React组件

  25. 25

    根据组件的使用位置,有条件地将className应用于react组件

  26. 26

    有条件地渲染JSX的React组件的更新周期如何表现?

  27. 27

    根据React功能组件中的状态变量有条件地设置className

  28. 28

    如何有条件地设置语义UI React Grid列的width属性?

  29. 29

    使用Firebase自定义声明有条件地在React Native中呈现UI

热门标签

归档