当传递给模型时,反应状态值变为空

问题描述 投票:0回答:1
您好先生,我具有AboutMeText状态,并且已成功通过props更新,我成功显示了它的值{this.state.AboutMeText}但是当我尝试将此状态值传递给模型时,其值变为null,我已经尝试过{this.state.AboutMeText}或{this.props.AboutMeText},但不起作用先生,请帮助我,我是React JS的新手
 <DIV className="template-profile">
                    <Image
                        url={this.state.selectedImages.profilePicture}
                        mode={"Profile"}
                    />
                    <h6>Hello I'm</h6>
                    <h1 className="template-profile-name">
                        Nabnit Jha
                    </h1>
                    <h6>
                    {this.state.AboutMeText} // here state value display successfully
                    </h6>
                    <DIV className="template-self-info">
                        <Modals modalBody={this.state.AboutMeText} modalHeading="About Me"></Modals>
                        //here state value became null
                    </DIV>
                </DIV>
javascript reactjs
1个回答
0
投票
如果我正确理解您的问题。您需要设置默认状态以避免数据为空

如果您在类的构造函数中使用类组件,则应设置状态的默认值

class SomeComponent extends React.Component { constructor(props) { super(props); this.state = { someData: [] }; } }

如果使用功能组件,则应使用react挂钩

const SomeComponent = () => { const [items, setItems] = useState([]); // default value empty array };

© www.soinside.com 2019 - 2024. All rights reserved.