New to React here. I am trying to reuse a component but changing a prop passed to a child component depending on the the component is attached to.
class HowMuchDay extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<p>How much a {this.props.time}</p> <br />
<h4 style= {h4Style}>{this.props.theValue}</h4>
</div>
);
}
}
class Display extends React.Component {
constructor(props) {
super(props);
this.state = {
input: "",
submit: ""
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleInput = this.handleInput.bind(this);
}
handleInput(event) {
this.setState({
input: event.target.value
});
}
handleSubmit(event) {
this.setState({
submit: this.state.input,
input: ""
});
event.preventDefault();
}
render() {
const formStyle = {
position: "relative",
textAlign: "center"
}
return (
<div style = {howStyle}>
<HowMuchDay theValue={this.state.submit} name={props.time}/>
<form onSubmit={this.handleSubmit} style= {formStyle}>
<input
value={this.state.input}
onChange={this.handleInput}
placeholder="how much?"
/>
<button type="submit">Submit</button>
</form>
</div>
);
}
}
ReactDOM.render(<Display time="day"/>, document.getElementById("day"));
ReactDOM.render(<Display time="week"/>, document.getElementById("week"));
ReactDOM.render(<Display time = "month"/>, document.getElementById("month"));
Sorry for the mess. So my first thought was maybe I could pass a prop into the ReactDOM.render itself but I don't think that would work. How would I go about this?