This issue is in React using TypeScript I have an array of components (Text inputs) sitting in my state. I also have an array of strings in my state that define the placeholder text of each component. Each component refers to the array of placeholder string from the state, at its corresponding index.
When I update a string within the placeholders array (using setState), the components do not update. Where am I going wrong? Or have I misunderstood how states/props work.
Thank you!
I have simplified the code a lot, to highlight the issue. Code:
interface IState {
inputComponents: IResponseOption[];
test_placeholder: string;
}
interface IResponseOption {
Component: JSX.Element;
InputValue: string;
}
class NewMultiQuestion extends React.Component<IProps, IState> {
constructor(props: any){
super(props);
if (this.props.location.props) {
this.state = {
inputComponents: [],
test_placeholder: "TESTING"
}
}
}
componentDidMount() {
this.generateInputElems();
}
generateInputElems() {
var createdInputOptions: IResponseOption[] = [];
for (var i = 0; i < 5; i++) {
var newInput: IResponseOption = {
Component: (
<div key={i}>
<TextInput key={i} id={i + ""} value="" placeholder={this.state.test_placeholder} hoverText={this.state.test_placeholder} />
</div>
),
InputValue: ""
}
createdInputOptions.push(newInput);
}
this.setState({inputComponents: createdInputOptions});
}
changeState() {
this.setState({test_placeholder: "Did it change?!"});
}
public render() {
let responseInputs = Object.keys(this.state.inputComponents).map((key) => {
return (this.state.inputComponents[key].Component);
});
return (
<div>
{responseInputs}
</div>
);
}
}
export default NewMultiQuestion;