victorzadorozhnyy
@victorzadorozhnyy

Почему не работают элементы props если их рендерить в один стейт React?

из родителя передаю метод и стейт, которые завязаны друг на друга. (нажимаем на кнопку с методом, он задает стейт)
Если делать все отдельно, то работает отлично
render() {
return (
    <div>
           
                            {this.state.subSectionCulturalSupport}
                            {this.props.PSSubSectionCulturalSupport}
                            
                            {this.state.subSectionFinancialSupport}
                            {this.props.PSSubSectionFinancialSupport}
                            
                            {this.state.subSectionCaseConference}
                            {this.props.PSSubSectionCaseConference}
                            
                            {this.state.subSectionDischargePlRef}
                            {this.props.PSSubSectionDischargePlRef}
                            
                            {this.state.subSectionCoordinateTravel}
                            {this.props.PSSubSectionCoordinateTravel}
                            
                            {this.state.subSectionPSDAMA}
                            {this.props.PSSubSectionPSDAMA}
    </div>

Хочу вывовить только релевантные пропсы и стейты, но когда пытаюсь присвоить все это одному стейту, то метод перестает работать с пропсами
componentWillMount(){
        
        if(this.props.chartName =='Patient Services') {

            this.setState({
                subSection: <div>
                    <Button block onClick={this.props.ConvertPDFSubReportCulturalSupport}>Add Cultural Support</Button>
                    {this.props.PSSubSectionCulturalSupport}
                    
                    <Button block onClick={this.props.ConvertPDFSubReportFinancialSupport}>Add FinancialSupport</Button>
                    {this.props.PSSubSectionFinancialSupport}
                    
                    <Button block onClick={this.props.ConvertPDFSubReportPSDAMA}>Add PSDAMA</Button>
                    {this.props.PSSubSectionPSDAMA}
                    
                    <Button block onClick={this.props.ConvertPDFSubReportCaseConference}>Add Case Conference</Button>
                    {this.props.PSSubSectionCaseConference}
                    
                    <Button block onClick={this.props.ConvertPDFSubReportDischargePlRef}>Add Discharge Planning/Referrals</Button>
                    {this.props.PSSubSectionDischargePlRef}
                    
                    <Button block onClick={this.props.ConvertPDFSubReportCoordinateTravel}>Add Coordinate Patient Travel</Button>
                    {this.props.PSSubSectionCoordinateTravel}
                </div>
            })
        }
    }

render() {
return (
    <div>
           //В этот стейт пытаюсь все задать, кнопки появляются, методы выполняются, но элементы из props перестали выводиться.
           {this.state.subSection}...


В чем моя ошибка?
  • Вопрос задан
  • 511 просмотров
Решения вопроса 1
@Aves
componentWillMount выполняется только один раз.
Для реагирования на изменение свойств надо такой же код добавить в componentWillReceiveProps.
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
theWaR_13
@theWaR_13
Возможно, что this указывает не туда, куда вы ожидаете. Попробуйте сделать в самом начале componentWillMount()
var _self = this, а внутри дивов вместо this.props пишите _self.props

UPD. А вообще, в React используются иные методы для того, чтобы при определенном условии отображать тот или иной html-код.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы