Background
I have a simple ReactJS 3-step form which consumers use to request tech support from our partners.
Until now, the support request form was universal for all of our partners (name, email, problem)
Now, I'd like to for a specific partner, add additional form elements. Ideally this should be customizable on a per-partner basis. Example, Partner A, would like to also capture the serial number of the device. Partner B would like to have the customer agree to a terms of service.
To solve this, I am passing along an optional component to my form. If there, it is rendered. I have not figured out how to access the optional components data, and pass it along as an opaque data field as part of the request submission.
Question
In the code below, in Form
how can I access the state of FormOverride
?
Or, is there a more idomatic way to achieve what I am looking for?
Unlike a parent / child hierarchy where a parent can access the child state in a number of ways, the FormOverride
component is instantiated outside of the Form
hierarchy, and passed as a property.
import React from "react";
const Form = ({ override }) => {
//universal form state
const handleSubmit = () => {
if (override) {
//get the state value from override component
}
alert("Clicked");
};
return (
<>
<h3>Contact Info</h3>
{override}
<h3>Support Details</h3>
<h3>Case Summary</h3>
<button onClick={handleSubmit}>Submit</button>
</>
);
};
const FormOverride = () => {
const [text, setText] = React.useState("");
const handleChange = (e) => {
setText(e.target.value);
};
return (
<div>
<input type="text" value={text} onChange={handleChange} />
</div>
);
};
export default function App() {
const someValue = "composed";
let toRender = null;
if (someValue === "default") {
toRender = <Form/>;
}
if (someValue === "composed") {
toRender = <Form override=<FormOverride /> />;
}
return <div className="App">{toRender}</div>;
}