简体   繁体   English

Function 在 React 中提交表单后不返回组件

[英]Function not returning component after form submit in React

I have been learning React for a couple of weeks now and I am trying to create a react app that displays a form and when submitted, it takes the inputs and creates a madlib sentence.我已经学习 React 几个星期了,我正在尝试创建一个显示表单的 React 应用程序,当提交时,它接受输入并创建一个 madlib 语句。 However when I submit the form, the GenerateMadlib component does not get rendered.但是,当我提交表单时,不会呈现 GenerateMadlib 组件。 Any help is appreciated.任何帮助表示赞赏。 ./Madlib.js ./Madlib.js

const Madlib = () => {
    const [formData, handleChange, resetFormData] = useFields({
        noun: '',
        noun2: '',
        adjective: '',
        color: ''
    })
    const handleSubmit = e => {
        e.preventDefault();
        resetFormData()
    }

    const generateMadlib = () => {
        console.log("This line runs");
        return <GenerateMadlib noun={formData.noun} noun2={formData.noun2} adjective={formData.adjective} color={formData.color} />
    }
    
    return (
        <div className="Madlibs">
            <h1>Madlibs!</h1>
            <form
            onSubmit={handleSubmit}>
                <input
                className="Madlibs-input"
                type="text" 
                name="noun" 
                value={formData.noun}
                placeholder="noun"
                onChange={handleChange}/>
                <input
                className="Madlibs-input"
                type="text" 
                name="noun2" 
                value={formData.noun2}
                placeholder="nou2" 
                onChange={handleChange}/>
                <input
                className="Madlibs-input"
                type="text" 
                name="adjective" 
                value={formData.adjective} 
                placeholder="adjective"
                onChange={handleChange}/>
                <input
                className="Madlibs-input"
                type="text" 
                name="color" 
                value={formData.color}
                placeholder="color"
                onChange={handleChange}/>
                <button onClick={generateMadlib}>send</button>
            </form>
        </div>
    )
}

./GenerateMadlib.js ./GenerateMadlib.js

const GenerateMadlib = ({noun, noun2, adjective, color}) => {
    const madlib = Sentencer.configure({
        nounList: [noun, noun2],
        adjectiveList: [adjective, color]
    });
    return (
        <>
            <h1>Madlibs!</h1>
            <p>{madlib}</p>
        </>
    )
}

Where do you want to show <Madlib /> component?你想在哪里显示<Madlib />组件? Button onClick callback function cannot return component because they don't know where to show returned component.按钮 onClick 回调 function 无法返回组件,因为他们不知道在哪里显示返回的组件。 You composite components as you build blocks.在构建块时组合组件。 Something like below code would work though I haven't tested yet.尽管我还没有测试过,但下面的代码会起作用。

const Madlib = () => {
    const [show, setShow] = useState(false)
    const [formData, handleChange, resetFormData] = useFields({
        noun: '',
        noun2: '',
        adjective: '',
        color: ''
    })
    const handleSubmit = e => {
        e.preventDefault();
        setShow(true)
        resetFormData()
    }
    
    return (
        <div className="Madlibs">
            <h1>Madlibs!</h1>
            <form>
                <input
                className="Madlibs-input"
                type="text" 
                name="noun" 
                value={formData.noun}
                placeholder="noun"
                onChange={handleChange}/>
                <input
                className="Madlibs-input"
                type="text" 
                name="noun2" 
                value={formData.noun2}
                placeholder="nou2" 
                onChange={handleChange}/>
                <input
                className="Madlibs-input"
                type="text" 
                name="adjective" 
                value={formData.adjective} 
                placeholder="adjective"
                onChange={handleChange}/>
                <input
                className="Madlibs-input"
                type="text" 
                name="color" 
                value={formData.color}
                placeholder="color"
                onChange={handleChange}/>
                <button onClick={handleSubmit}>send</button>
            </form>
            {show && <GenerateMadlib noun={formData.noun} noun2={formData.noun2} adjective={formData.adjective} color={formData.color} />}
        </div>
    )
}

Or you may want to redirect using react-router-dom.或者您可能想使用 react-router-dom 进行重定向。

Returning a component from an onClick callback will not append the component to the DOM.onClick回调返回组件不会 append 组件到 DOM。 Only what returns from the functional component will be appended to the DOM.只有从功能组件返回的内容才会附加到 DOM。 So instead you should create an array that contains all the forms generated or a value that contains the current form generated and render that array via JSX.因此,您应该创建一个数组,其中包含所有生成的 forms 或包含当前生成的表单的值,并通过 JSX 呈现该数组。

const Madlib = () => {
    const [formData, handleChange, resetFormData] = useFields({
        noun: '',
        noun2: '',
        adjective: '',
        color: ''
    })
    const [currentForms, setCurrentForms] = useState([])

    const handleSubmit = e => {
        e.preventDefault();
        resetFormData()
    }

    const generateMadlib = () => {
        console.log("This line runs");
        setCurrentForms(
            currentForms.push(
                (<GenerateMadlib noun={formData.noun} noun2={formData.noun2} adjective={formData.adjective} color={formData.color} />)
            )
        ) //? This updates state and re-renders with new forms
    }

    var currentForms = []
    
    return (
        <div className="Madlibs">
            <h1>Madlibs!</h1>
            <form
            onSubmit={handleSubmit}>
                <input
                className="Madlibs-input"
                type="text" 
                name="noun" 
                value={formData.noun}
                placeholder="noun"
                onChange={handleChange}/>
                <input
                className="Madlibs-input"
                type="text" 
                name="noun2" 
                value={formData.noun2}
                placeholder="nou2" 
                onChange={handleChange}/>
                <input
                className="Madlibs-input"
                type="text" 
                name="adjective" 
                value={formData.adjective} 
                placeholder="adjective"
                onChange={handleChange}/>
                <input
                className="Madlibs-input"
                type="text" 
                name="color" 
                value={formData.color}
                placeholder="color"
                onChange={handleChange}/>
                <button onClick={generateMadlib}>send</button>
            </form>
            {currentForms}
        </div>
    )
}

Note: I haven't tested this code so it might have errors注意:我没有测试过这段代码,所以它可能有错误

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

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