简体   繁体   English

如何在reactJS中呈现条件形式?

[英]How to render conditional form in reactJS?

Conditionally, when having in imported array attribute called ' entry ', I want to render form ' write ', but ' write ' is not displayed in the browser (no errors in console).有条件地,当导入名为“ entry ”的数组属性时,我想呈现表单“ write ”,但浏览器中未显示“ write ”(控制台中没有错误)。 Should I use child component for this, or maybe you have ideas for other approaches?我应该为此使用子组件,还是您对其他方法有想法?

The code:编码:

render() {
        var replyList = questions.map(reply => {
            return (
                reply.feedback.map(singleReply => {
                    return (
                        <div>
                            <button
                                key={singleReply.id}
                                value={singleReply.button}
                                goto={singleReply.goto}
                                onClick={this.onButtonClick}>
                                {singleReply.button}
                            </button>
                        </div>
                    );
                })
            );
        });
        var write = (evt) => {
            //argument dla input
            var toWrite = questions[this.state.currentDialog].entry;
            //jeśli jest entry'
            if (questions[this.state.currentDialog].entry)
                return (
                    <form onSubmit={this.onInputSubmit}>
                        <label value={toWrite.label} />
                        <input
                            name={toWrite.name}
                            value={toWrite.value}
                            onChange={this.onInputChange}
                        />
                        <input type='submit' />
                    </form>
                );
        };
        return (
            //questions - pytanie, replyList - lista odpowiedzi
            <div className="App">
                {questions[this.state.currentDialog].question}
                <br /><br />

                {replyList[this.state.currentDialog]}
                {this.write}
                <br /><br />

            </div>)
    }

Piece of my array:我的阵列的一部分:

{
        //[0]
        id: uuid.v4(),
        question: 'dialog1',
        feedback: [
            { button: 'reply1-1', goto: 1, id: uuid.v4() },
            { button: 'reply1-2', goto: 2, id: uuid.v4() },
            { button: 'reply1-3', goto: 3, id: uuid.v4() },
            { button: 'reply1-4', goto: 4, id: uuid.v4() }
        ],
        entry: { label: 'input1-1', name: 'input1', value: '1', id: uuid.v4() }
    },

Inorder to display the write you need to call it as为了显示write您需要将其称为

  return (
            <div className="App">
                {questions[this.state.currentDialog].question}
                <br /><br />

                {replyList[this.state.currentDialog]}
                {write()}
                <br /><br />
            </div>)

this is not required since the write is defined inside the render method.You should also keep in mind the problem with putting functions inside render method. this不是必需的,因为write是在 render 方法中定义的。您还应该记住将函数放入 render 方法中的问题。

A function in the render method will be created each render which is a slight performance hit. render 方法中的一个函数将在每次渲染时创建,这是一个轻微的性能损失。 It's also messy if you put them in the render, which is a much bigger reason, you shouldn't have to scroll through code in render to see the html output.如果你把它们放在渲染中也会很混乱,这是一个更大的原因,你不应该在渲染中滚动代码来查看 html 输出。 Always put them on the class instead.总是把它们放在课堂上。

write is part of the local scope for render, no need to call this.write . write 是局部渲染作用域的一部分,不需要调用this.write simply call write .只需调用write More on this you have to call the function as well: write()关于这一点,您还必须调用该函数: write()

To add to this, not really part of the question but you will get an error.除此之外,这不是问题的真正组成部分,但您会收到错误消息。 Every component has to return a 'component-like' value.每个组件都必须返回一个“类似组件”的值。 If the condition is not fulfilled the write function will return undefined which will throw an error.如果条件不满足,write 函数将返回 undefined ,这将引发错误。 Returning null will not throw an error as it's 'component-like'返回 null 不会抛出错误,因为它是“类似组件的”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM