我很难理解使用React Context API的新方法。我有一个带有自定义类Firebase的应用程序。现在我想勾住它。在我使用HOC (高阶组件)和上下文之前。
我的问题
这是我的代码,其中包含与问题相关的一些注释
// context.js this is my hoc
// index.jsx
import App from './App'
import Firebase, { FirebaseContext } from './components/Firebase'
const FirebaseContext = React.createContext(null)
export const withFirebase = Component => (props) => {
// I don't need to wrap it to the FirebaseContext.Consumer
// 1 But do I need this HOC or it's a new way?
const firebase = useContext(FirebaseContext)
return <Component {...props} firebase={firebase} />
}
ReactDOM.render(
// 2 …Run Code Online (Sandbox Code Playgroud) 我正在使用LinerSVC技术对文本进行分类,但我希望得到每个预测附加的预测置信度.
这就是我现在所拥有的:
train_set = self.read_training_files()
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform([e[0] for e in train_set])
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
clf = LinearSVC(C=1).fit(X_train_tfidf, [e[1] for e in train_set])
_ = text_clf.fit([e[0] for e in train_set], [e[1] for e in train_set])
foods = list(self.get_foods())
lenfoods = len(foods)
i = 0
for food in foods:
fd = self.get_modified_food(food)
food_desc = fd['fields']['title'].replace(',', '').lower()
X_new_counts = count_vect.transform([food_desc])
X_new_tfidf = tfidf_transformer.transform(X_new_counts)
predicted = clf.predict(X_new_tfidf)
Run Code Online (Sandbox Code Playgroud)
我想将UI LiveData与DataBinding一起使用时进行更改。但是随着LiveData变化,它并不能反映UI的变化。
MainViewModel.java
public class MainViewModel extends AndroidViewModel {
/**
* Boolean variable to see whether user is ClockedIn or ClockedOut
*/
public MutableLiveData<Boolean> isClockedIn;
public MainViewModel(Application application) {
super(application);
isClockedIn = new MutableLiveData<>();
//...
calculateClockedInOrNot(PreferencesManager.getString(mContext,PreferencesManager.CLOCK_STATUS_KEY));
}
/**
* An utility function to calculate whether user is clocked in or clocked out,
* this function is also called within this ViewModel after an api hit
*
* if string == "CLOCKED_OUT" or string == "mobileEventType.CLOCK_OUT" => user …Run Code Online (Sandbox Code Playgroud) android android-studio android-databinding android-architecture-components
android ×1
android-architecture-components ×1
javascript ×1
python ×1
react-hooks ×1
reactjs ×1
scikit-learn ×1