Skip to content

When using For to iterate and create TextField, the TextField rerenders and loses focus #327

@thep0y

Description

@thep0y

Native input elements do not have this issue.

import { For } from "solid-js";
import { createStore } from "solid-js/store";

import { TextField } from "@suid/material";

function App() {
	const [textFieldInputs, setTextFieldInputs] = createStore<string[]>([""]);
	const [nativeInputs, setNativeInputs] = createStore<string[]>([""]);

	const updateTextFieldInput = (index: number, value: string) => {
		setTextFieldInputs(index, value);
	};

	const updateNativeInput = (index: number, value: string) => {
		setNativeInputs(index, value);
	};

	return (
		<div>
			<h4>TextField</h4>
			<For each={textFieldInputs}>
				{(input, index) => (
					<TextField
						value={input}
						onChange={(e) => updateTextFieldInput(index(), e.target.value)}
						size="small"
					/>
				)}
			</For>

			<h4>Native Input</h4>
			<For each={nativeInputs}>
				{(input, index) => (
					<input
						value={input}
						onChange={(e) => updateNativeInput(index(), e.target.value)}
						size="small"
					/>
				)}
			</For>
		</div>
	);
}

export default App;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions