state

swift - ForEach loop with state

元气小坏坏 提交于 2021-01-29 07:10:48
问题 If I have this code: @State var n = 3 var body: some View { VStack { Button(action:{ if self.n == 3 { self.n = 5 } else { self.n = 3 } }) { Text("Click me") } ForEach(1..<self.n+1) { i in Text(String(i)) } } } I expect this code to toggle between 1 2 3 and 1 2 3 4 5 on the screen when you click the button, however it only shows 1 2 3 . How would I fix this? 回答1: The form of ForEach that you have used is fine if the number of items is constant, but if the Views might change, then you need to

flutter: taking screenshots of a widget from its sibling

时光总嘲笑我的痴心妄想 提交于 2021-01-29 06:17:12
问题 Goal I have a view that has two children Widget A and Widget B, standing side by side. Widget B has a button that triggers a screenshot on Widget A, Problem but referring to questions such as this one, it seems that the implementation of screen capture must stay within Widget A. So the button callback in Widget B needs to talk to Widget A. Since both are stateful widgets, I find it difficult for B to call A's function inside its state. The various provider and inheritedWidget mechanisms seem

React.js (w/ Electron) unmounted component state change warning

蹲街弑〆低调 提交于 2021-01-29 06:14:49
问题 I'm currently building an Electron app with React, and I keep getting an error that my state is changing while my component isn't mounted. Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. in Start (created by Context.Consumer) I've looked up multiple threads and questions about this, and there are no async tasks or

React Redux double mount with useEffect cleanup of previous component

时光怂恿深爱的人放手 提交于 2021-01-29 06:13:54
问题 I'm using react-redux, and I'm trying to clear part of my redux state when a component unmounts, this actually does work, but the problem is that once userProfile is cleared and next component mounts it is unmounted and then mounted again. useEffect(() => { getProfileByUsername(match.params.username, user && user.id); return () => clearUserProfile(); }, [match.params.username]) I can see my clear action is being called before any mount of the next component occurs, but the data is not cleared

Passing input state from parent to child component

风流意气都作罢 提交于 2021-01-28 20:11:41
问题 I want to pass the value and setValue to Right component . I've done something but it's not working.I am typing but it's deleting immediately.I can't see even what I am typing to textinput.What is the proper way to do this ? export const Vault = ({ navigation }: VaultStackNavigationProps<"Vault">) => { const [value, setValue] = useState(""); React.useLayoutEffect(() => { navigation.setOptions({ headerRight: () => ( <Right setText={setValue} value={value} /> ), }); }, [navigation]); return ( /

ReactJS: Should a parent component's fetched data be stored in state to pass to children?

你说的曾经没有我的故事 提交于 2021-01-28 09:12:22
问题 When I am in a situation where I need pass a significant size of data to a child component, it seems the easiest way to do this is to store it in state and then pass it to children as props inside the render function. I am wondering if there is a less-intensive way to do this. For instance, if I have a relationship like so: <PhotosPage> <PhotoFeed/> </PhotosPage> I know that I want to fetch the 'photo feed' data when the PhotosPage mounts. Let's say I fetch this data in PhotoPage 's

How to maintain state using react-router

怎甘沉沦 提交于 2021-01-27 21:44:24
问题 I have a react app with a main App.js file that holds my initial state. I then have some routes setup to navigate around my application. In one of the routes I have a button which when pressed processes a set state. I know that this works as I've console logged the change in state. However, it then also calls: window.location.href = '/'; This then routes us to our homepage. However, once the homepage renders the state reverts to its initial setup as detailed in App.js. I don't know why state

Flutter GetIt Plugin - No type xxx is registered inside GetIt

放肆的年华 提交于 2021-01-27 19:13:34
问题 I set everything up as shown in the example project: import 'package:get_it/get_it.dart'; import 'package:places/services/authService.dart'; final locator = GetIt.instance; void setupLocator() { locator.registerSingleton<AuthService>(AuthService()); print("registered"); } with the call in the main file void main() { setupLocator(); runApp(MyApp()); } I have some Check where the locator also correctly return my AuthService class AuthGuardView extends StatefulWidget { AuthGuardView({Key key}) :

How to update parent state and child components props at once (at the same time)

為{幸葍}努か 提交于 2021-01-27 07:07:58
问题 I'm new to React and basically I'm trying to update a parent App.js components' state and its child components ( Team.js and Player.js ) props at once. Currently only the parent components' state is being updated. I will try to explain it better with a step by step. Here I have a parent component App.js export default function App() { const teams = [ { Name: "Chicago Bulls", Players: ["Michael Jordan", "Dennis Rodman", "Scottie Pippen"], Championships: 6 }, { Name: "Golden State Warriors",

Passing asynchronously acquired data to child props

独自空忆成欢 提交于 2021-01-27 06:52:39
问题 I am making an app that gets an array of news items from a remote source and displays them on a page. I have the endpoint, and can make a successful call, proven by console logs, using $.getJSON() . I placed this call into the parent component, because child components will need to use the data. However, when I pass this data down to a child component, the console error appears: Uncaught TypeError: Cannot read property 'headline' of undefined This is because React is trying to render the