小编San*_*eev的帖子

什么是使用新的React钩子useContext的正确方法?

我很难理解使用React Context API的新方法。我有一个带有自定义类Firebase的应用程序。现在我想勾住它。在我使用HOC (高阶组件)和上下文之前。

我的问题

  1. 我需要使用HOC还是一种新方法?
  2. 我需要Context.Provider还是新的Hook?
  3. 我需要将默认值声明为null还是可以从context.js中正确传递对象
  4. 如何在我的代码中使用新的Hook代替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)

javascript reactjs react-context react-hooks

3
推荐指数
1
解决办法
1613
查看次数

使用scikit LinearSVC预测置信度

我正在使用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)

变量"预测"将包含预测的类别编号,其中不包括置信水平.我一直在这里阅读源代码,但我没有找到适当的属性来执行此操作.

python scikit-learn

2
推荐指数
1
解决办法
2009
查看次数

当与DataBinding一起使用时,LiveData不会将更改通知给UI

我想将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

0
推荐指数
1
解决办法
1565
查看次数