reactjs - 如何通过上下文 Api 反应传递状态?
问题描述
首先,我创建了一个 .js 文件并创建了上下文。
在 app.js 中
export default function App({ navigation }) {
const [ItemURL,setItemURL] = useState('URL')
return (
<ItemContext.Provider value={ItemURL}>
...
</ItemContext.Provider>
)
}
现在我想将我的传递setItemURL
给我的子组件
所以我尝试了。
export default const ItemsComponent = (props) => {
const [URL, setURL] = useContext(ItemContext)
return(
<TouchableWithoutFeedback
onPress={() => {
setURL(props.Json.Image)
}}
/>
)
}
但它不起作用并且说setURL is not a function(in setURL(props.Json.Image)) ,setURL is 'R'
解决方案
您实际上也应该setURL
在上下文值中传递函数。
export default function App({ navigation }) {
const [ItemURL, setItemURL] = useState('URL');
const value = [ItemURL, setItemURL];
return (
<ItemContext.Provider value={value}>
...
</ItemContext.Provider>
)
}
推荐阅读
- c++ - 在 C 中找到对应的类型
- jquery - 如何解析双引号 CSV 值并分配给 JSON 对象?
- selenium - 在 Selenium 中使用 Chrome 无头浏览器启动 Google 页面时出现 NosuchelementException
- c# - Nlog 存档不能以编程方式工作
- c# - 无法使用通过 StackExchange.Redis 返回 RedisResult 的方法对类进行单元测试
- python - 从其他列中的值获取列索引 nr
- python - Python BeautifulSoup 如何获取坐标
- slack - 使用今天的日期自定义松弛提醒消息
- mysql - 带有 SUM() 和多行的子查询的 Max() 值
- python - 为什么 pipenv shell 不被识别