How can i make this condition dynamic?

fieldName is a variable and is dynamic and the value can be either media, media2 or media3. How can i make "media" be either media, media1 or media3. instead of doing this… if(fieldName === ‘media’) { const newObj = { …objComponent, media: json.media }; } if(fieldName === ‘media2’) { const newObj = { …objComponent, media2:… Read More How can i make this condition dynamic?

How do I add search functionality to this to-do list?

Want to search through added todos and display only searched todos. function App() { const [text, setText] = useState("Add a task") const [task, setTask] = useState(getLocalItem()) const changeText = (event) => { setText(event.target.value) } const submitHandler = (event) => { console.log("submitted"); event.preventDefault(); setTask([…task, text]) setText("") } const removeTask =(a)=>{ const finalData = task.filter((curEle,index)=>{ return index… Read More How do I add search functionality to this to-do list?

TS2322: Type '{ userName: string; setUserName: React.Dispatch<React.SetStateAction<string>>; }' is not assignable to type 'string'

I’m trying to use ReactContext for global state management. I have copied the code from this example: https://codesandbox.io/s/update-context-value-l39t0?file=/src/App.js Yet typescript is throwing the following error: TS2322: Type ‘{ userName: string; setUserName: React.Dispatch<React.SetStateAction<string>>; }’ is not assignable to type ‘string’. Attached is my code: const UserContext = createContext(‘Unknown’); export default function App() { const [accessibilityMode, setAccessibilityMode]… Read More TS2322: Type '{ userName: string; setUserName: React.Dispatch<React.SetStateAction<string>>; }' is not assignable to type 'string'

how to create a list with stateful components in react?

I’m quite new to react, and I have this problem that when trying to map an array to a list of React stateful component, all my components lose their state. My code: const { useState, useEffect} = React; const htmlRoot = document.getElementById(‘root’); const reactRoot = ReactDOM.createRoot(htmlRoot); const App = () => { const Item =… Read More how to create a list with stateful components in react?