使用useState(function)初始化Context中的变量,该函数在函数返回值时获取值。然后,应用程序的组件可以访问此值。这里的问题是先安装组件,然后再获取数据,而我希望它在呈现组件之前先获取数据。
我的上下文存储
function AuthContextProvider(props){
var s;
const fetchData=()=> {
userSession.getFile('st.json')
.then((file) => {
s= JSON.parse(file || "")
setState(s
)
})
return s
}
const [userType,setState]=useState(s)
index.js
function Auth () {
const {userType} =useContext(AuthContext)
const auth=()=>{
if (userSession.isSignInPending()) {
userSession.handlePendingSignIn()
.then((response) => {
if({userType}==="Hire"){window.location.href = "/dashboard";}
else if({userType}==="Dev"){
window.location.href = "/dashboard";
}
else{
window.location.href = "/profilef";
}
});
}
}
return null;
在此条件被执行,然后从useState()获取数据 相反,我希望它获取数据然后执行条件
答案 0 :(得分:0)
类似的东西吗?
我将userType初始化为''(空字符串)
class Tester
def select
loop do
puts
puts "Will we search for the file by name(1), extension(2), name extension(3) or exit programm(4)?"
print "\n>>>>>> "
input = gets.chomp
search_method = "search_#{input}"
if (respond_to?(search_method))
contents = send(search_method)
contents.each do |txt_name|
z_name = File.basename(txt_name) # file name
path = File.expand_path(txt_name) # path file
new_name_file[z_name] = path # everything in the hash
end
else
puts "Unknown input: #{input.inspect}, method #{search_method} not defined."
end
end
end
end