我正在尝试react-native-google-places-autocomplete在 React Native 应用程序中使用该库,但在单击建议列表时遇到问题。每当我点击 Google 建议的地址时,列表就会消失并且onPress不会获取地址。如果我在没有父组件的情况下单独在组件中使用库,它工作得很好,但只有当我将组件放在keyboardavoidingviewor 中时KeyboardAwareScrollView,它才不起作用:
这个 GooglePlacesInput.js 组件只是来自原始存储库的标准库:
<GooglePlacesAutocomplete
placeholder='Search'
minLength={2} // minimum length of text to search
autoFocus={false}
returnKeyType={'search'} // Can be left out for default return key https://facebook.github.io/react-native/docs/textinput.html#returnkeytype
keyboardAppearance={'light'} // Can be left out for default keyboardAppearance https://facebook.github.io/react-native/docs/textinput.html#keyboardappearance
listViewDisplayed={false} // true/false/undefined
fetchDetails={true}
renderDescription={row => row.description} // custom description render
onPress={(data, details = null) => { // 'details' is provided when fetchDetails = true
console.log(data, details);
}} …Run Code Online (Sandbox Code Playgroud) 我正在将类组件转换为函数组件,想看看是否useEffect()可以替换以下静态函数
static getDerivedStateFromProps(props) {
const { path } = props.match;
if (path === '/login') {
return { loginStatus: true };
} else {
return { loginStatus: false };
}
}
Run Code Online (Sandbox Code Playgroud)
以下是新的替换功能。它正在做它应该做的事情,即根据道具更改状态,但我对 useEffect() 不太熟悉,我只是想知道转换为 this 是否不会丢失任何东西。
const { path } = props.match
useEffect(() => {
if (path ==='/login'){
setLoginStatus(true)
} else {
setLoginStatus(false)
}
}, [ path ])
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个自定义 Hook,它允许我传递一个值并在按下按钮时减去 1。我目前收到的错误是
React Hook“useLastPage”在函数“handleLastPage”中调用,该函数既不是 React 函数组件,也不是自定义 React Hook 函数
在下面的代码中:
function usePrevPage (page) {
const [lastPage, useLastPage] = useState(page)
useEffect(() => {
function handleLastPage(page) {
useLastPage(page - 1)
}
handleLastPage()
})
return lastPage
}
Run Code Online (Sandbox Code Playgroud)
我的代码密切反映了 React Doc 的自定义钩子示例,因此我不确定如何useLastPage在我的自定义钩子中调用。以下是React 文档中的示例:
function useFriendStatus(friendID) {
const [isOnline, setIsOnline] = useState(null);
useEffect(() => {
function handleStatusChange(status) {
setIsOnline(status.isOnline);
}
ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange);
return () => {
ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange);
};
});
return isOnline;
}
Run Code Online (Sandbox Code Playgroud) 我有一个输入Formik,需要输入数字,但即使它通过了Formik验证,结果输入仍然被分类为string.
<Formik
initialValues={{ price: '' }}
onSubmit={submitHandler}
validationSchema={yup.object().shape({
price: yup
.number()
.required(),
})}
>
{({ values, handleChange, errors, setFieldTouched, touched, isValid, handleSubmit }) => (
<View style={styles.form}>
<View style={styles.fieldset}>
<Text style={{ marginLeft: 40, marginVertical: 10 }}>
<Text style={{ color: '#FF5D4E'}}>* </Text>
Price
</Text>
<TextInput
value={values.price}
keyboardType = 'numeric'
onChangeText={handleChange('price')}
placeholder="Rental price of your item per day"
style={styles.textInput}
onBlur={() => setFieldTouched('price')}
/>
</View>
{touched.price && errors.price &&
<Text style={{ fontSize: 10, color: 'red' }}>{errors.price}</Text>
} …Run Code Online (Sandbox Code Playgroud) 我正在使用 React 的钩子,我希望有一个从数据库中检索到的值作为初始值。但是,我收到以下错误:
Invariant Violation:Invariant Violation:比上一次渲染时渲染了更多的钩子。
const { data, loading, error } = useQuery(GET_DATA)
const { initialValue } = data
const [value, setValue] = useState(initialValue)
Run Code Online (Sandbox Code Playgroud)
我正在使用 React Apollo 钩子。
更新
export default NotificationScreen = ({ navigation }) => {
const { data: initialNotificationSettings, loading: loadingInitialSettings, error: initialSettingsError } = useQuery(GET_NOTIFICATION_SETTINGS)
if (loadingInitialSettings) {
return (
<View style={[styles.container, styles.horizontal]}>
<ActivityIndicator size="large" color="#FF5D4E" />
</View>
)
}
if (initialSettingsError) return <Text>Error...</Text>
const {
borrowerLendingNotificationToken,
} = initialNotificationSettings.me
const [borrowerPending, notifyBorrowerPending] = useState(borrowerLendingNotificationToken) …Run Code Online (Sandbox Code Playgroud) 我目前正在使用ERC721PresetMinterPauserAutoId作为智能合约,并在 Node.js 后端服务器中使用 Web3.js 库。当我尝试使用此Web3 API调用mint函数时:
var myContract = new web3.eth.Contract(ERC721PresetMinterPauserAutoIdABI, ERC721PresetMinterPauserAutoIdContractAddress, {
from: from,
gasPrice: gasPrice
});
let result;
try {
result = await myContract.methods.mint(receipientAddress).send();
res.status(201).send(result)
} catch (error) {
res.status(201).send(error)
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
返回错误:方法 eth_sendTransaction 不存在/不可用
我通过 Infura 网关与 Rinkeby 区块链进行通信,根据这篇文章,Infura 仅支持eth_sendRawTransaction,不支持eth_sendTransaction。
我能够使用签名交易成功发送以太币:
const gasPrice = await web3.eth.getGasPrice()
const txCount = await web3.eth.getTransactionCount(from, 'pending')
var rawTx = {
nonce: txCount,
gasPrice:"0x" + gasPrice,
gasLimit: '0x200000',
to: …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 React-Native 中使用现代 Context API,但出现以下错误:
类型错误:类型错误:未定义不是对象(评估 'Context._context')
我的 createDataContext.js:
import React, { useReducer } from 'react'
export default (reducer, actions, defaultValue) => {
const Context = React.createContext()
const Provider = ({ children }) => {
const [state, dispatch] = useReducer(reducer, defaultValue)
const boundActions = {}
for (let key in actions) {
boundActions[key] = actions[key](dispatch)
}
return (
<Context.Provider value={{ state, ...boundActions }}>
{children}
</Context.Provider>
)
}
return { Context, Provider }
}
Run Code Online (Sandbox Code Playgroud)
我的context.js:
import { AsyncStorage } from 'react-native'
import …Run Code Online (Sandbox Code Playgroud) 我目前正在使用 React Navigation 5.x,并且我有一个向下滑动以关闭的模式,并且它已经在ScrollView上面了。问题是,由于可以通过向下手势滚动内容,因此模式有时会意外关闭。所以我想动态禁用gestureEnabled为 false 直到onScrollToTop指示ScrollView已到达顶部。
<Main.Navigator
tabBarPosition="none"
initialRouteName="Main"
screenOptions={{
headerShown: false,
gestureEnabled: true,
gestureResponseDistance: {
horizontal: width,
vertical: height,
},
}}
mode="modal"
headerMode="none"
>
// screens
</Main.Navigator>
Run Code Online (Sandbox Code Playgroud)
如何传递道具以动态screenOptions禁用gestureEnabled?或者有更好的方法来解决这个问题吗?
我看到的大多数使用 Open Zeppelin 的 ERC721 示例都要求 mint 函数具有访问控制,仅允许合约所有者调用该函数。例如,
function mint(address to) public virtual {
require(hasRole(MINTER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have minter role to mint");
_mint(to, _tokenIdTracker.current());
_tokenIdTracker.increment();
}
Run Code Online (Sandbox Code Playgroud)
或使用Ownable库进行以下操作。
function mint(address receiver) external onlyOwner returns (uint256) {
_tokenIds.increment();
uint256 newTokenId = _tokenIds.current();
_mint(receiver, newTokenId);
return newTokenId;
}
Run Code Online (Sandbox Code Playgroud)
这是否意味着每次铸造新代币时都必须部署新合约?这不仅在 Gas 费用方面显得过高,而且 ERC721 合约具有映射不同所有者和代币的属性:
// Mapping from token ID to owner address
mapping (uint256 => address) private _owners;
// Mapping owner address to token count
mapping (address => uint256) private …Run Code Online (Sandbox Code Playgroud) 我正在尝试添加一个选项来回复对帖子的评论,但我一直收到:
CommentRepliesController@createReply 未定义。
通过添加对帖子的回复PostCommentsController@store效果很好。但是,当我尝试通过返回帖子或直接comment/reply在 URL 中添加对评论的回复时,它给了我上面的错误。
以下是我的路线:
Route::group(['middleware'=>'auth'], function(){
Route::resource('comment/reply', 'CommentRepliesController@createReply');
});
Run Code Online (Sandbox Code Playgroud)
以下是我的CommentRepliesController@createReply:
public function createReply(Request $request){
$user = Auth::user();
if($user->photo){
$data = [
'comment_id' => $request->comment_id,
'author' => $user->name,
'email' => $user->email,
'photo' => $user->photo->file,
'body' => $request->body
];
} else{
$data = [
'comment_id' => $request->comment_id,
'author' => $user->name,
'email' => $user->email,
'body' => $request->body
];
}
CommentReply::create($data);
$request->session()->flash('reply_message', 'Your reply has been submitted
and is awaiting moderation.');
return redirect()->back();
}
Run Code Online (Sandbox Code Playgroud)
以下是我的 …
react-native ×5
reactjs ×5
javascript ×4
react-hooks ×4
ethereum ×2
solidity ×2
blockchain ×1
expo ×1
formik ×1
ios ×1
lamp ×1
laravel ×1
nft ×1
openzeppelin ×1
php ×1
react-apollo ×1
scrollview ×1
sql ×1
web3js ×1