Answer the question
In order to leave comments, you need to log in
Why doesn't props of modal components change on update from external setState?
There was a problem in passing data from the system configuration steps component to the modal window.
So, given
1) Modal window. Exprotit 3 classes.
- the modal window itself - React.Component
- wrapper of the class passing props from the call options for program rendering
- wrapper of the class passing props with spread {...this.props} - also React.Component, using the portal api
That is, it should be ok. I can render a modal and functionally - from some event, and start on state in a jsx template.
2) System setup steps component
This is the stuffing of the section. It contains a complex state. In the current version, it is all described manually, but a little later the server will drive up and the state will be merged from the props that apollo will place.
this.state = {
steps: {
[stepName]: {
status: "proccess",
forcedOpen: false,
summary: true,
places: {
[placeName]: {
label: "label",
logged: true,
openModal: false // это способ рендера <ModalRenderer/> по условию bool state в шаблоне,
logFields: {
[fieldName]: {
value: 'val'
}
}
}
}
}
ё
/* Это кусок render-функции корневого компонента роута, в котором проблема*/
render() {
...
const renderLoginModalInners = (place, stepKey, placeKey) => {
if (stepKey !== 'alreadyHasProfile') return false;
return <div className='flex flex-col'>
{Object.keys(place.logFields).map(key => {
if (key === 'clientID')console.log(this.state.steps[stepKey].places[placeKey].logFields[key].value)
// let field = place.logFields[key];
return <Input key={key}
value={this.state.steps[stepKey].places[placeKey].logFields[key].value}
onChange={this.bindPlaceLogField.bind(this, stepKey, placeKey, key)}/>
})}
<Button disabled={!this.isAllPlaceFieldsFilled.bind(this, place)}><Trans i18nKey='auth.action.logon'/></Button>
</div>
}
...
const renderLogger = (place, stepKey, placeKey) => {
if (!place.openLogger) return;
return <ModalRenderer ...>{renderLoginModalInners(place, stepKey, placeKey)}</ModalRenderer>
}
}
bindPlaceLogField(stepKey, placeKey, fieldKey, event) {
const value = event.target.value
this.setState((state) => {
console.log(state.steps[stepKey].places[placeKey].logFields[fieldKey].value)
state.steps[stepKey].places[placeKey].logFields[fieldKey].value = value;
return Object.assign(state)
}, () => {
console.log(this.state.steps[stepKey].places[placeKey].logFields[fieldKey].value)
})
}
bindSwitchPlace(stepKey, placeKey, value) {
this.setState((state) => {
let newState = Object.assign(state);
newState.steps[stepKey].places[placeKey].value = value;
return newState;
})
}
toggleLoginModal(stepKey, placeKey, value) {
this.setState((state) => {
state.steps[stepKey].places[placeKey].openLogger = value;
return Object.assign(state)
})
/*вариант программного рендера. Сначала я отрендерил компонент программно, и подумал что в этом причина */
// const {t} = this.props;
// new ModalCaller({
// title: `${t('markets.step.loginModal.title')} ${place.label}`,
// children: this.renderLoginModalInners(place, stepKey, placeKey), эта функция была методом компонента и возвращала кусочек jsx
// })
}
Answer the question
In order to leave comments, you need to log in
Guys, everything is ok (almost)
When I described the problem in detail - well, you know, the duck method.
I took a liking to an input that had input value={this.props.value} and decided to try passing the value through the state, and it worked. Although it's strange - inputs in other sections are updated normally.
Can someone explain to me what is happening here in React?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question