alibaba-fusion / next

🦍 A configurable component library for web built on React.
https://fusion.design
MIT License
4.59k stars 591 forks source link

[Field] autoUnmount开启时,组件Unmount后,field还能取得这个key #1469

Closed cwtuan closed 3 years ago

cwtuan commented 4 years ago

Component

Field

Reproduction link

https://riddle.alibaba-inc.com/riddles/27e81388

import React from 'react@16';
import ReactDOM from 'react-dom@16';
import { Input, Select, Range, Field } from '@alife/next@1.x';

class App extends React.Component {
    field = new Field(this, {
        autoUnmount: true,
        onChange: (name, value) => {
            console.log(this.field.getValues());
        }
    });

    render() {
        const {init, getValue} = this.field;
        const layout = {
            marginBottom: 10,
            width: 400
        };

        return (<div>
                        下拉1
            <Select style={layout} {...init('select1', {initValue: 'a'})}>
                <Select.Option value="a">a</Select.Option>
                <Select.Option value="b">b</Select.Option>
                <Select.Option value="c">c</Select.Option>
            </Select><br/>

            输入1
            {
                getValue('select1') == 'a' &&
                    <Input                     
                        {...init('input1', {initValue: "d"}) }
                    />
            }
            <br/>

            输入2
            {
                getValue('input1') == 'd' &&
                    <Input                     
                        {...init('input2', {initValue: "e"}) }
                    />
            }

        </div>);
    }
}

ReactDOM.render(<App/>, mountNode);

Steps to reproduce

将"下拉1"选成"b" 这时候"输入1"消失,符合预期。 但是field的值居然有input1,正常情况应该不能有input1 image

应此也导致了input2没有消失(input2依赖了input1) image

kotot commented 4 years ago

同样的问题,还有 required 的组件,被UNmount后,校验还是会出错,原因应该是一样的

bindoon commented 4 years ago

比较难处理,因为组件的 unmount 不是立刻进行的,是 rerender 后的某个时机才执行的。所以立刻就使用确实还存在。暂时无解

cwtuan commented 4 years ago

是不是只能onChange后,setTimeout(this.forceUpdate, 1000) 这样hack一下了?

bindoon commented 3 years ago

是不是只能onChange后,setTimeout(this.forceUpdate, 1000) 这样hack一下了?

确实如此,React unmount 是异步的,Field 也是等 unmount 时候才能更新数据