[英]How to pass state from one component to another that are joined by a stacknavigator in react native?
Let's say I have two components, A and B that are joined together by the stack navigator that are being rendered in App.js.假设我有两个组件 A 和 B,它们由在 App.js 中呈现的堆栈导航器连接在一起。 Now in AI have declared some state variables and also in BI have declared some state variables.
现在在 AI 中声明了一些 state 变量,并且在 BI 中也声明了一些 state 变量。 I want to use the state variables I declared in A in B. And then use all the state variables of A and B together to test an api endpoint.
我想使用我在 B 中的 A 中声明的 state 变量。然后将 A 和 B 的所有 state 变量一起使用来测试 api 端点。
For eg: Componenet A:例如:组件 A:
...
const A = () => {
const [projectName, setProjectName] = useState("");
const [projectDescription, setProjectDescription] = useState("");
const [projectBudget, setProjectBudget] = useState(null);
const [projectDuration, setProjectDuration] = useState(null);
const [industry, setIndustry] = useState("");
const [companyName, setCompanyName] = useState("");
const [numberOfEmployees, setNumberOfEmployees] = useState(null);
return (
...
);
}
...
...
const B = () => {
var newDiamond = [
{ newCriteria: { novelty: "", technology: "", complexity: "", pace: "" } },
];
...
const [techVal, setTechVal] = useState("");
const [noveltyVal, setNoveltyVal] = useState("");
const [complexityVal, setComplexityVal] = useState("");
const [paceVal, setPaceVal] = useState("");
return (
...
newDiamond[0].newCriteria.technology = techVal;
...
newDiamond[0].newCriteria.novelty = noveltyVal;
...
newDiamond[0].newCriteria.complexity = complexityVal;
...
newDiamond[0].newCriteria.pace = paceVal;
...
//Need to initiate an HTTP request here by using state variables of both A and B components:
axios.post('/projects', {projectName, projectDescription, projectBudget, projectDuration, industry, companyName, numberOfEmployees, newDiamond});
//newDiamond that is initialized in B uses state variables of B, is used in the request.
...
);
}
...
How can I pass state from A to B?如何将 state 从 A 传递到 B? I don't think there is a parent-child relationship between them so I don't think I can pass props b/w them.
我不认为他们之间有亲子关系,所以我认为我不能通过它们来传递道具。
You should consider using React Context API https://uk.reactjs.org/docs/context.html .您应该考虑使用 React Context API https://uk.reactjs.org/docs/context.Z5E056FDC70D22766 Its dedicated to sharing the common state (
items
in your case).它致力于共享常见的 state(您的案例中的
items
)。 Here is an example: You should create a common context for items: ItemsState.js这是一个示例:您应该为项目创建一个公共上下文:ItemsState.js
import React, { useState, useContext } from 'react';
const ItemsContext = React.createContext([]);
export const ItemsProvider = ({ children }) => {
return (
<ItemsContext.Provider value={useState([])}>
{children}
</ItemsContext.Provider>
);
}
export const useItems = () => useContext(ItemsContext);
Then share the context between screens with provider in App.js like this然后像这样在 App.js 中与提供者共享屏幕之间的上下文
import {ItemsProvider} from 'ItemsState';
function App() {
return (
<ItemsProvider> // share the items between both screens
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Add" component={AddScreen} />
</Stack.Navigator>
</NavigationContainer>
</ItemsProvider>
);
}
Then use items context in each screen like this AddScreen.js然后在每个屏幕中使用项目上下文,如 AddScreen.js
import {useItems} from './ItemsState';
function AddScreen({ route, navigation }) {
const [items, setItems] = useItems(); // <- using items context as global useState
const [itemName, setItemName] = React.useState('');
const [itemPrice, setItemPrice] = React.useState('0');
const addItem = () => {
setItems([...items, { itemName, itemPrice }]);
setItemName('');
setItemPrice('0');
};
return (
<View>
<TextInput
multiline
placeholder="What's on your mind?"
value={itemName}
onChangeText={setItemName}
/>
<TextInput
multiline
placeholder="What's on your mind?"
value={itemPrice}
onChangeText={setItemPrice}
/>
<Button
title="Done"
onPress={() => {
addItem();
// Pass params back to home screen
navigation.navigate('Home', items);
}}
/>
</View>
);
}
You can also use useReducer
hook and make more Redux-like.你也可以使用
useReducer
钩子来制作更多类似 Redux 的东西。 Check out this article https://medium.com/simply/state-management-with-react-hooks-and-context-api-at-10-lines-of-code-baf6be8302c查看这篇文章https://medium.com/simply/state-management-with-react-hooks-and-context-api-at-10-lines-of-code-baf6be8302c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.