标题在分开的一瞬间消失。

huangapple go评论50阅读模式
英文:

header disappears after a split second

问题

以下是您的代码:

class AppModal extends Component {
    static propTypes = {
        noDocs: PropTypes.bool
    };

    constructor(props) {
        super(props);
        this.state = {
            catSelected: false
        }
    }

    onChange = value => {
        this.setState({
            catSelected: true
        })
    }

    render() {
        const header = (
            <div>
                {
                    this.state.catSelected && this.props.noDocs && (
                        <p>
                            selected req don't apply
                        </p>
                    )
                }
            </div>
        );

        return (
            <Modal
                header={header}>
                <form>
                    {ADD_DOC.map(item => (
                        <Row key={item}>
                            <Col>
                                <Field
                                    component="input"
                                    onChange={(e, val) => this.onChange(val)}
                                />
                            </Col>
                        </Row>
                    ))}
                </form>
            </Modal>
        )
    }
}

const mapStateToProps = (state, props) => {
    const initialValues = {
        noDocs: true
    }
    let noDocs = formValueSelector('addDoc')(state, 'noDocs');
    noDocs = !!noDocs
    return {
        noDocs
    };
}; 

export default connect(mapStateToProps)(
    reduxForm()(AppModal)
);

关于您的问题,您想要确保标题仅在 this.state.catSelected && this.props.noDocs 为 true 时显示。如果标题在不应显示时短暂出现然后消失,这可能是由于组件的重新渲染导致的。

您可以尝试在 render 方法的开始处添加一个条件,以确保只有在 this.state.catSelected && this.props.noDocs 为 true 时才渲染标题:

render() {
    const shouldRenderHeader = this.state.catSelected && this.props.noDocs;

    const header = (
        <div>
            {shouldRenderHeader && (
                <p>
                    selected req don't apply
                </p>
            )}
        </div>
    );

    // ...其余代码...
}

这将确保标题只在满足条件时才渲染,避免了在不应显示时短暂出现和消失的问题。

英文:

below is my code

I have an AppMdal Component where i need to show the header when both catSelected and noDocs are true.

For some reason, It works as expected but when the header is not supposed to show , it appears for a split second and then disappears. How do i stop that ?

ADD_DOC is an array of items. I made sure that that's always there. There's no other CSS rules that might cause the issue

class AppModal extends Component {
static propTypes = {
noDocs :this.propTypes.bool
};
constructor(props) {
super(props);
this.state = {
catSelected: false
}
}
onChange= value =&gt; {
this.setState({
catSelected: true
})
}
render() {
const header = (
&lt;div&gt;
{
this.state.catSelected &amp;&amp; this.props.noDocs &amp;&amp; (
&lt;p&gt;
selected req don&#39;t apply
&lt;/p&gt;
)
}
&lt;/div&gt;
);
return (
&lt;Modal
header={header}&gt;
&lt;form&gt;
{ADD_DOC.map(item =&gt; (
&lt;Row&gt;
&lt;Col&gt;
&lt;Field&gt;
component={input}
onChange={(e, val) =&gt; this.onChange(val)}
&lt;/Field&gt;
&lt;/Col&gt;
&lt;/Row&gt;
))}
&lt;/form&gt;
&lt;/Modal&gt;
)
}
}
const mapStateToProps = (state, props) =&gt; {
const initialValues={
noDocs = true
}
let noDocs = formValueSelector(&#39;addDoc&#39;)(state, &#39;noDocs&#39;);
noDocs = !!noDocs
return {
noDocs
};
}; 
export default connect(mapStateToProps) {
reduxForm()(AppModal)
}

I cannot get my head around why the header appears and disappears for a split second. How do i avoid it. header should show only when this.state.catSelected && this.props.noDocs is true.

答案1

得分: 1

将组件的状态中的catSelected初始化为true,而不是false。这将防止初始时显示标题。

constructor(props) {
  super(props)
  this.state = {
    catSelected: true
  }
}

更新onChange方法,以根据value参数设置catSelected。这确保了当值发生变化时,catSelected会正确更新。

onChange = value => {
  this.setState({
    catSelected: !!value
  })
}

只有当catSelected和noDocs都为true时才渲染标题,这可以防止它在短暂地出现和消失。

render() {
  const header =
    this.state.catSelected && this.props.noDocs ? (
      <div>
        <p>selected req don't apply</p>
      </div>
    ) : null

  // 你的其余代码...
}
英文:

Initialize catSelected in the component's state to true instead of false. This will prevent the header from being shown initially.

constructor(props) {
  super(props)
  this.state = {
    catSelected: true
  }
}

Update the onChange method to set catSelected based on the value parameter. This ensures that catSelected is updated correctly when the value changes.

onChange = value =&gt; {
  this.setState({
    catSelected: !!value
  })
}

Render the header only when both catSelected and noDocs are true, which prevents it from briefly appearing and disappearing.

render() {
  const header =
    this.state.catSelected &amp;&amp; this.props.noDocs ? (
      &lt;div&gt;
        &lt;p&gt;selected req don&#39;t apply&lt;/p&gt;
      &lt;/div&gt;
    ) : null

  // Rest of your code...
}

答案2

得分: 0

Nazrul Chowdhury的答案有效,但消息会显示在初始视图上,这不是我想要的。

我观察到在我的mapStateToProps中为'redux'状态值'noDocs'设置了'initialValues'。

当我在initialValues中将noDocs设置为false时,我能够停止出现和消失标题。

下面也修复了这个问题:

const initialValues = {
  noDocs: false
}
英文:

Answer from Nazrul Chowdhury works but the message shows on the initial view which i don't want.

I have observed that there is 'initialValues' for 'noDocs' redux state value set in my mapStateToProps.

when i set noDocs to false in initialValues, i was able to stop appearing and disappearing the header.

below also fixed the issue

const initialValues = {
noDocs = false 
}

huangapple
  • 本文由 发表于 2023年7月14日 03:03:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定