使用API值作为道具来更改React中的状态

samar2170

我正在使用API​​值primaryKey更改Click上表示的数据,但该功能似乎无法正常工作。并且不会引发任何错误。我无法在这里找到问题所在。

我在这里想要做的是-默认情况下,表格输出的每只股票都具有多列,单击按钮时,它应该使用该股票的键值来仅代表具有该列的那只股票。这是我的部分代码:

    handleClick = (props) => {
        return (
            <div>
                {this.state.data.filter(data => data.includes({props})).map(filteredData => (
                    <li>
                    {filteredData}
                    </li>
            ))};
            </div>
        );
    }
    renderArray = () => {
        return (
            <table className="table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Price</th>
                        <th>Price/ Chng</th>
                        <th>Mkt Cap</th>
                        <th>Volume</th>
                        <th>Turnover</th>
                    </tr>
                </thead>
                <tbody>
                    {this.state.data.map(item => {
                        return (
                            <tr key={item.co_S}>
                                <button onCLick={this.setState = () => this.handleClick(item.co_S)}><td >{item.co_N}</td></button>
                                <td>{item.price}</td>
                                <td>{item.p_chng_pc}</td>
                                <td>{item.Mkt_cap}</td>
                                <td>{item.volume}</td>
                                <td>{item.volume * item.price}</td>
                            </tr>
                        );
                    })};
                    
                </tbody>
            </table>
        );
    }
    render() {
        return (
            <this.renderArray />
        )
    }
}

export default StocksHomePage2;
穆罕默德·伊利亚斯

class App extends React.Component {
  state = {
    data: [
      {
        co_S: 1,
        co_N: 1,
        price: 100,
        volume: 20,
      },
      {
        co_N: 2,
        co_S: 2,
        price: 30,
        volume: 7,
      },
    ],
  };

  handleClick = (props) => {
    this.setState({
      data: this.state.data.filter((item) => item.co_S === props),
    });
  };

  renderArray = () => {
    return (
      <table className="table">
        <thead>
          <tr>
            <th>Name</th>
            <th>Price</th>
            <th>Price/ Chng</th>
            <th>Mkt Cap</th>
            <th>Volume</th>
            <th>Turnover</th>
          </tr>
        </thead>
        <tbody>
          {this.state.data.map((item) => {
            return (
              <tr key={item.co_S}>
                <button onClick={() => this.handleClick(item.co_S)}>
                  <td>{item.co_N}</td>
                </button>
                <td>{item.price}</td>
                <td>{item.p_chng_pc}</td>
                <td>{item.Mkt_cap}</td>
                <td>{item.volume}</td>
                <td>{item.volume * item.price}</td>
              </tr>
            );
          })}
          
        </tbody>
      </table>
    );
  };
  render() {
    return this.renderArray();
  }
}

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过状态作为道具

道具更改状态在React Form中更新

将道具传递给子组件作为React中的状态

在React中传递状态作为道具

在React componentDidUpdate中,道具和状态可以同时更改吗?

在道具中设置React组件的状态,并在道具更改时更新?

通过函数作为道具来更新React中父组件中的状态

当React Hooks中的redux状态更改时,道具不更新

在React中使用'for'作为道具

为什么道具在地图上无法通过地图状态更改为React Redux中的道具

我无法使用componentDidMount中的设置状态来更改状态

React rerender道具更改与本地状态更改

R:使用一个变量中的值作为新变量来更改数据帧结构

不能使用状态值作为子组件的道具

使用道具从子类访问/更改父状态(React)

使用 Redux 更改根组件的状态/道具?

使用状态中的值作为组件文本

如何编写脚本任务来测试将键盘状态作为道具传递的 React 组件

Flow - 如何使用从函数中获得的值来设置状态?

更改 reactJs 父组件中的状态不会更改 react 子组件中的道具

更改视频 react-native-video 组件中的状态/道具

无法使用 React Native 中作为道具传递的值进行计算

我们如何使用 React Native 中的 navigationOptions 在功能组件中将状态作为道具传递?

使用上下文 API (React.js) 仅更改状态对象中的特定键

如何提取文本输入值并将该值作为道具传递给子组件以用于在 React 中获取第三方 api 数据?

使用道具更改打字稿中的状态

使用按钮 React 从搜索栏中的道具更新状态

React - 作为道具传递时,父级的状态更改不会导致重新渲染子级?

使用打字稿在 React 路由器 v6 中将状态作为道具传递