【发布时间】:2022-08-17 03:22:05
【问题描述】:
将组件的渲染方法拆分为被视为反模式的函数吗?
我的意思是,如果 JSX 变得太大,我们可以完美地将其拆分为更多组件......
但是,下面的例子呢:
/**
* Renders the text inputs of the form.
*
* @returns {React.ReactElement} The text inputs.
*/
const renderInputs = () => (
<View style={styles.inputsContainer}>
<UsernameInput
ref={usernameInputRef}
label={t(
\"authentication.signUp.accountInformation.usernameInputLabel\"
)}
returnKeyType=\"next\"
onChange={handleOnTextInputChange}
onSubmitEditing={() => emailInputRef.current.focus()}
containerStyle={commonStyles.textInputContainer}
/>
<TextInput
ref={emailInputRef}
label={t(\"authentication.signUp.accountInformation.emailInputLabel\")}
maxLength={MAX_EMAIL_LENGTH}
textContentType=\"emailAddress\"
keyboardType=\"email-address\"
returnKeyType=\"next\"
icon={{
name: \"email\",
type: \"material\",
color: colors.scorpion,
}}
onChange={handleOnTextInputChange}
onSubmitEditing={() => passwordInputRef.current.focus()}
containerStyle={commonStyles.textInputContainer}
/>
<PasswordInput
ref={passwordInputRef}
label={t(
\"authentication.signUp.accountInformation.passwordInputLabel\"
)}
textContentType=\"newPassword\"
returnKeyType=\"next\"
onChange={handleOnTextInputChange}
onSubmitEditing={() => repeatPasswordInputRef.current.focus()}
containerStyle={commonStyles.textInputContainer}
/>
<PasswordInput
ref={repeatPasswordInputRef}
label={t(
\"authentication.signUp.accountInformation.repeatPasswordInputLabel\"
)}
textContentType=\"oneTimeCode\"
returnKeyType=\"done\"
blurOnSubmit
onChange={handleOnTextInputChange}
containerStyle={commonStyles.textInputContainer}
/>
</View>
);
/**
* Renders a button for continuing to the next screen.
*
* @returns {React.ReactElement} The *\'continue\'* button.
*/
const renderContinueButton = () => (
<Button
disabled={isContinueDisabled}
uppercase
mode=\"contained\"
onPress={handleOnContinue}
style={styles.button}
labelStyle={globalStyles.buttonLabel}
>
{t(\"authentication.signUp.accountInformation.continueButton\")}
</Button>
);
return (
<View style={globalStyles.flexHorizontallyCenteredContainer}>
{renderInputs()}
{renderContinueButton()}
</View>
);
}
我应该避免在这里拆分代码吗?正如你所看到的......我正在为大多数“原子”部分使用自定义组件......以及两个内部辅助方法来使用相应的布局调整来呈现它们。
模式还是反模式?
标签: javascript reactjs react-native design-patterns