账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    React 父组件向子组件传值时无限刷数据
    23
    0

    最近在学习react,想做一个todo试试。但是在父组件向子组件传递值的时候遇到了问题

    父组件App:

    class App extends Component {
      constructor(props){
        super(props);
        this.state = {
          set:[{title:'测试',content:'第一个',isOn:true}]
        }
        this.handleSave = this.handleSave.bind(this);
      }
      handleSave(set){
        this.setState({set:[set]})
      }
      render() {
        console.log("看我渲染了几次")
        return (
          <div className="App">
            <div className="App-header">
              <p className="App-title">Fidlcn's Todo</p>
            </div>
            <div className='todoBox'>
                  <Add onSave={this.handleSave} />
                  <Content states = {this.state} />
            </div>
          </div>
        );
      }
    }
    

    子组件content:

    class Content extends Component{
        constructor(props){
            super(props);
            this.state = { set:[this.props.states.set[0]]};
            this.handleBtnStatusChange = this.handleBtnStatusChange.bind(this);
        }
        
        shouldComponentUpdate(nextProps, nextState) {
            if( nextProps.states.set.title !== "undefined"){
                this.setState({set:[nextProps.states.set[0]]})
                console.log(nextProps.states)
                return true
            }
        }
        handleBtnStatusChange(e){
            console.log(e);
            console.log(this);
        }
        render(){
            return(
                <div  className='content'>
                    <ul className='ulList'>
                    {
                        this.state.set.map((item,i)=>{
                            let isOn = item.isOn;
                            return (
                                <li key={i}>
                                    <span>{i+1}</span>
                                    <div className='ulDiv'>
                                        <h3>{item.title}</h3>
                                        {item.content}
                                    </div>
                                    <div className='ulBtn'>
                                        {isOn ? (
                                            <input type="button" value="Y" />):(
                                            <input type="button" value="Y" disabled />
                                        )}
                                        <input type='button'  value="N" onClick={this.handleBtnStatusChange} />
                                    </div>
                                </li>
                            )
                        })
                    }
                    </ul>
                </div>
            )
        }
    }
    

    子组件add:

    import React from 'react';
    import { Button, Input } from 'antd'
    import 'antd/dist/antd.css';
    import './common.css';
    import '../App.css';
    
    const { TextArea } = Input;
    
    class Add extends React.Component{
        constructor(props){
            super(props);
            this.handleUpload = this.handleUpload.bind(this);
            this.handleSaveT = this.handleSaveT.bind(this);
            this.handleSaveC = this.handleSaveC.bind(this);
            this.state = {title:'',content:'',isOn:true};
        }
        handleSaveT(e){
            this.setState({title:e.target.value})
        }
        handleSaveC(e){
            this.setState({content:e.target.value});
            this.setState({isOn:true});
        }
        handleUpload(){
            this.props.onSave(this.state)
        }
        render(){
            console.log("add渲染了几次")
            return(
                <div className="Add">
                    <h2>Todo内容添加</h2>
                    <div className='inputArea'>
                        <Input addonBefore="标题" onChange={this.handleSaveT} />
                        <TextArea rows={18} id='titleInput' placeholder='在此输入内容' onChange={this.handleSaveC} />
                    </div>
                    <Button type="primary" onClick={this.handleUpload}>添加</Button>
                </div>
            )
        }
    }
    
    export default Add;
    
    

    想法是add里输入值,点击Button将值传递给父组件,父组将值传递给子组件content的state来触发更新,但实际情况是点击add里的button就会报错,可以看出是父组件更改content值的问题,但不知道具体该怎么修改(图不知道为什么没法上传)

    Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
        
      12 | 
      13 | shouldComponentUpdate(nextProps, nextState) {
      14 |     if( nextProps.states.set.title !== "undefined"){
    > 15 |         this.setState({set:[nextProps.states.set[0]]})
      16 |         console.log(nextProps.states)
      17 |         return true
      18 |     }
    
    
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 苦撐 普通会员 1楼

      在React中,父组件向子组件传值时,如果数据量过大,可能会导致子组件无限刷数据。为了解决这个问题,可以使用useRefuseEffect两个钩子函数。useRef函数用于创建一个只读的引用,而useEffect函数用于执行副作用代码。

      以下是一个例子:

      ```jsx import React, { useRef, useEffect } from 'react';

      function ParentComponent() { const ref = useRef(null);

      useEffect(() => { // 这里是你的业务逻辑 // 如果你的数据量非常大,可以考虑将数据分批加载到子组件 const batchData = [ { key: '1', value: 'value1' }, { key: '2', value: 'value2' }, // ... ]; ref.current.batchData = batchData; }, []);

      return (

      {/ 子组件 /}
      ); }

      export default ParentComponent; ```

      在这个例子中,父组件在useEffect中,通过ref.current.batchData获取子组件的数据。如果子组件的数据量非常大,可以考虑将数据分批加载到子组件,而不是一次性加载到所有数据。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部