N
N
Nikita Kit2019-10-09 21:18:27
React
Nikita Kit, 2019-10-09 21:18:27

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'
                                }
                            }
                        }
                    }
                }

Everywhere where the key is in square brackets, there are neighboring objects under different keys. In order to change the state by pressing a switcher or a button, I received it via halfback, copied it, changed it and returned it back. And it worked everywhere, and when I attached an input to this state in a modal window, onChange of this input fires, makes changes to the state, and even the render function is executed again, but the prop value in the Input does not change
. I get the feeling that something I misunderstood setState
ё
/* Это кусок 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>
        }
}

Well, setState, which I don’t know how to make friends with associative arrays
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
        // })
   }

all console.logs are processed at the right time - onchange calls if (key === 'clientID') console.log and value is already changed there, as in the updated setState callback, but the input itself is not redrawn.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Kit, 2019-10-09
@ShadowOfCasper

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 question

Ask a Question

731 491 924 answers to any question